Giter VIP home page Giter VIP logo

gocron's Introduction

Note from current maintainers:

A currently maintained fork of this project has been migrated to https://github.com/go-co-op/gocron

Disclaimer: we (the maintainers) tried, with no luck, to get in contact with Jason (the repository owner) in order to add new maintainers or leave the project within an organization. Unfortunately, he hasn't replied for months now (March, 2020).

So, we decided to move the project to a new repository (as stated above), in order to keep the evolution of the project coming from as many people as possible. Feel free to reach over!

goCron: A Golang Job Scheduling Package.

This package is currently looking for new maintainers (cause @jasonlvhit is in ICU). Please message @jasonlvhit if you are interested.

GgoDoc Go Report Card

goCron is a Golang job scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.

goCron is a Golang implementation of Ruby module clockwork and Python job scheduling package schedule, and personally, this package is my first Golang program, just for fun and practice.

See also this two great articles:

If you want to chat, you can find us at Slack!

Back to this package, you could just use this simple API as below, to run a cron scheduler.

package main

import (
	"fmt"
	"time"

	"github.com/jasonlvhit/gocron"
)

func task() {
	fmt.Println("I am running task.")
}

func taskWithParams(a int, b string) {
	fmt.Println(a, b)
}

func main() {
	// Do jobs without params
	gocron.Every(1).Second().Do(task)
	gocron.Every(2).Seconds().Do(task)
	gocron.Every(1).Minute().Do(task)
	gocron.Every(2).Minutes().Do(task)
	gocron.Every(1).Hour().Do(task)
	gocron.Every(2).Hours().Do(task)
	gocron.Every(1).Day().Do(task)
	gocron.Every(2).Days().Do(task)
	gocron.Every(1).Week().Do(task)
	gocron.Every(2).Weeks().Do(task)

	// Do jobs with params
	gocron.Every(1).Second().Do(taskWithParams, 1, "hello")

	// Do jobs on specific weekday
	gocron.Every(1).Monday().Do(task)
	gocron.Every(1).Thursday().Do(task)

	// Do a job at a specific time - 'hour:min:sec' - seconds optional
	gocron.Every(1).Day().At("10:30").Do(task)
	gocron.Every(1).Monday().At("18:30").Do(task)
	gocron.Every(1).Tuesday().At("18:30:59").Do(task)

	// Begin job immediately upon start
	gocron.Every(1).Hour().From(gocron.NextTick()).Do(task)

	// Begin job at a specific date/time
	t := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.Local)
	gocron.Every(1).Hour().From(&t).Do(task)

	// NextRun gets the next running time
	_, time := gocron.NextRun()
	fmt.Println(time)

	// Remove a specific job
	gocron.Remove(task)

	// Clear all scheduled jobs
	gocron.Clear()

	// Start all the pending jobs
	<- gocron.Start()

	// also, you can create a new scheduler
	// to run two schedulers concurrently
	s := gocron.NewScheduler()
	s.Every(3).Seconds().Do(task)
	<- s.Start()
}

and full test cases and document will be coming soon (help is wanted! If you want to contribute, pull requests are welcome).

If you need to prevent a job from running at the same time from multiple cron instances (like running a cron app from multiple servers), you can provide a Locker implementation and lock the required jobs.

gocron.SetLocker(lockerImplementation)
gocron.Every(1).Hour().Lock().Do(task)

Once again, thanks to the great works of Ruby clockwork and Python schedule package. BSD license is used, see the file License for detail.

Looking to contribute? Try to follow these guidelines:

  • Use issues for everything
  • For a small change, just send a PR!
  • For bigger changes, please open an issue for discussion before sending a PR.
  • PRs should have: tests, documentation and examples (if it makes sense)
  • You can also contribute by:
    • Reporting issues
    • Suggesting new features or enhancements
    • Improving/fixing documentation

Have fun!

gocron's People

Contributors

alexrios avatar arjunmahishi avatar bir avatar claudiu avatar gljubojevic avatar jasonlvhit avatar jchorl avatar jkc avatar johnroesler avatar kaiiak avatar kilhage avatar leventebo avatar marksalpeter avatar muesli avatar nitper avatar preslavrachev avatar sadeghpro avatar streppel avatar tiger5226 avatar wishdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gocron's Issues

Interval doesn't work correctly

I try to run a task every 3 seconds, but it still runs every second:

func Hello() {
	fmt.Printf("hello %d\n", time.Now().Unix())
}

func main() {
	gocron.Every(3).Seconds().Do(Hello)
	<-gocron.Start()
}

The result is:

hello 1561223112
hello 1561223113
hello 1561223114
hello 1561223115
hello 1561223116
hello 1561223117
hello 1561223118

Cannot Run Job on Sunday 8:00 AM UTC

I have added logs to my application whenever it runs my Job to make sure it has properly executed.

When I schedule my Job on Sunday 8:00 AM UTC, it fails to execute (no logs generated):

// Run function every Sunday at 8:00 AM UTC
go func() {
    gocron.ChangeLoc(time.UTC)    
    gocron.Every(1).Sunday().At("08:00").Do(MyFunction)        
    <- gocron.Start()
}()

However, if I schedule the Job on a different day (e.g. Thursday), it executes (logs were generated):

// Run function every Thursday at 8:00 AM UTC
go func() {
    gocron.ChangeLoc(time.UTC)    
    gocron.Every(1).Thursday().At("08:00").Do(MyFunction)        
    <- gocron.Start()
}()

Is there something I am doing wrong when scheduling my Job on Sunday 8:00 AM UTC, or is there an issue with using gocron for this specific interval?

Support for standard Cron like definitions

Is there any way to use the "standard" cron expressions with this package ?

something like:

gocron.Parse("00 07 * * *").Do(its7amTask())

The use case is to have the user define it's own logic from a config file.

Multithreaded Cron

Note: This is something that I've already forked code and am looking at the possibility of implementing.

What I've noticed is that the cron does not run multi threaded. They each run linearly. EG:

I have a two queries to a DB. Both should be set at the same time. But, from what I can tell, query 1 runs first, query 2 runs a short time after. Not really what I'm wanting in a scheduler.

Ideally:

I'd like to have as many threads as I specify to run at a time. I'd like to be able to set a limit so I don't collapse a system if I have thousands of queries.

News

Hi, are there any new developments to extend the existing code?

Thanks!

Jobs with unique ID's

I made some changes that allow you to schedule jobs with unique ID's. I suppose it depends on your use-case whether or not you'd want to use this. If you're interested, I can make a pull-request.

Example:

func main() {
    s := gocron.NewScheduler()
    // Do jobs with params
    s.Job("8c1f99f3-2b6e-4fdb-9656-b50c91bfa740").Every(1).Second().Do(taskWithParams, 1, "hello")

    // Do jobs without params
    s.Job("abc").Every(1).Second().Do(task)
    s.Job("def").Every(2).Seconds().Do(task)
    s.Job("ghi").Every(1).Minute().Do(task)
    s.Remove("ghi")
    <-s.Start()

Write Tests

The following scenarios need test written before version 1 can ship. Check the v1 branch for progress

Job

  • Job.Every(...).Day.At(...)
  • Job.Every(...).Weekday(...).At(...)
  • Job.Every(...).Hour()
  • Job.Every(...).Minute()
  • Job.Every(...).Second()

Scheduler

  • Scheduler.runPending(...)
  • Scheduler.Start(...)
  • Scheduler.IsRunning(...)
  • Scheduler.Stop(...)
  • Scheduler.Remove(...)
  • Scheduler.Clear(...)
  • Scheduler.Location(...)
  • Scheduler.NextRun(...)
  • Scheduler.RunAll(...)
  • Scheduler.RunPending(...)
  • Scheduler.RunAllWithDelay(...)

Do I have to use <- gocron.Start()

I was just looking at the examples on the readme and I just wanted to clarify the purpose of <- gocron.Start()

If I have:

gocron.Every(1).Second().Do(task)

Will that work as is? Or do I need to run <- gocron.Start() to initiate it?

Day of the Week not working?

I have written a simple program that writes hello at a certain time on both Monday and Friday:

package main
import (
  "fmt"
  "github.com/jasonlvhit/gocron"
)

func main() {
  gocron.Every(1).Monday().At("15:54").Do(func() { fmt.Println("Hi from Monday!") })
  gocron.Every(1).Friday().At("15:54").Do(func() { fmt.Println("Hi from Friday!") })
  <-gocron.Start()
}

The output today (Friday) at 15:54 on my OS X machine was this:

Hi from Monday!
Hi from Friday!

Also, it is not relevant that the two times were the same. This

  gocron.Every(1).Monday().At("16:13").Do(func() { fmt.Println("Hi from Friday!") })
  gocron.Every(1).Friday().At("16:14").Do(func() { fmt.Println("Hi from Monday!") })

produced this by 16:14:

Hi from Friday!
Hi from Monday!

Any ideas?

Could not run job at specific time when daylight saving time switches

Code:

gocron_test.go

package main

import (
	"time"
	"fmt"
	"github.com/jasonlvhit/gocron"
)

func main() {
	fmt.Println("start main")
	go func() {
		for {
			time.Sleep(time.Duration(3*time.Second))
			now := time.Now()
			fmt.Println("	(now", now, time.Local, ")")
		}
	}()
	gocron.Every(1).Day().At("01:59").Do(pri1)
	gocron.Every(1).Day().At("03:01").Do(pri2)
	<- gocron.Start()
}

func pri1() {
	now := time.Now()
	fmt.Println("do 1 !!!", now)
}

func pri2() {
	now := time.Now()
	fmt.Println("do 2 !!!", now)
}

The time zone is set to America/New_York, and the date time is set to 2017-03-12 01:58:50 by sudo timedatectl set-time "2017-03-12 01:58:50" && sudo hwclock -w

Output:

[vagrant@localhost ~]$ ./settime.sh && go run ./test_gocron.go
Sun Mar 12 01:58:51 EST 2017
start main
	(now 2017-03-12 01:58:54.789104654 -0500 EST Local )
	(now 2017-03-12 01:58:57.78959527 -0500 EST Local )
	(now 2017-03-12 01:59:00.793605829 -0500 EST Local )
do 1 !!! 2017-03-12 01:59:00.793731129 -0500 EST
	(now 2017-03-12 01:59:03.797613207 -0500 EST Local )
	(now 2017-03-12 01:59:06.798436061 -0500 EST Local )
	...
	(now 2017-03-12 03:00:51.833313996 -0400 EDT Local )
	(now 2017-03-12 03:00:54.83476427 -0400 EDT Local )
	(now 2017-03-12 03:00:57.836203538 -0400 EDT Local )
	(now 2017-03-12 03:01:00.838035849 -0400 EDT Local )
	(now 2017-03-12 03:01:03.839273531 -0400 EDT Local )
	(now 2017-03-12 03:01:06.840455311 -0400 EDT Local )

The output shows that gocron.Every(1).Day().At("01:59").Do(pri1) runs properly, but gocron.Every(1).Day().At("03:01").Do(pri2) doesn't take any effect.

P.S. after 2017-03-12 01:59:59, daylight saving time switches from EST to EDT

Run every hour at defined minute

Hi,
looking at code and examples there is a way to do a task every day at fixed time gocron.Every(1).Day().At("10:30").Do(task)
but if I must do a task every hour at fixed minute how can do?

Thanks
Maurizio

Could not run specific intervals

When I try the schedule for 1 second interval task, but the interval shows bellow wasn't stable!

$ go run cron.go 
now task is runing!!  2018-01-25 02:38:31
now task is runing!!  2018-01-25 02:38:33
now task is runing!!  2018-01-25 02:38:35
now task is runing!!  2018-01-25 02:38:36
now task is runing!!  2018-01-25 02:38:38
now task is runing!!  2018-01-25 02:38:40
now task is runing!!  2018-01-25 02:38:41
now task is runing!!  2018-01-25 02:38:43
now task is runing!!  2018-01-25 02:38:44
now task is runing!!  2018-01-25 02:38:46
now task is runing!!  2018-01-25 02:38:47
now task is runing!!  2018-01-25 02:38:48
now task is runing!!  2018-01-25 02:38:50

The code is :

func DemoJobs() {
    s := gocron.NewScheduler()
    s.Every(1).Seconds().Do(func() {
        fmt.Println("now task is runing!! ",  time.Now().Format("2006-01-02 15:04:05"))
    })

    <- s.Start()
}

Can gocron work with gin

I want to use gocron with gin router. But it seems that gin router and gocron block each other.
If i start goncron before router.Run, the genData will be done, but can not get response from /ex/getdata. If i start router.Run before gocron.Start(), I can get response from /ex/getdata but genData job is not executed.

func main() {
	data := &Data{}
	router := gin.Default()
	router.GET("/ex/getdata", getData)
	
	gocron.Every(1).Second().Do(genData)
	<-gocron.Start()
	router.Run(":8888")
}

At() seems to be not working

gocron.Every(1).Day().At("9:00").Do(task)

The task gets run every day at time of scheduling execution instead of every day at specified timing

gocron crashes while scheduling large number of events

to reproduce add following to main in example.go

	for ind := 0; ind < 10000; ind++ {
		gocron.Every(1).Minute().Do(task)
	}

This fails with error:

panic: runtime error: index out of range

goroutine 1 [running]:
main.main()
	/GOPATH/goscheduler/examples/example.go:24 +0xbbe

I have tried slowing the for loop by adding time.Sleep(1*time.Second), still it fails.

Scheduled job gets delayed by the running time of the job in a long running (daemon like) program

I have written a little tool to backup my database every day at 0:00 and 13:00, thus:

gocron.Every(1).Day().At("00:00").Do(mainRoutine)
gocron.Every(1).Day().At("13:00").Do(mainRoutine)
<-gocron.Start()

The backup action itself takes around 10~12 seconds. I have set up a logger inside the program, and these are the log output:

INFO[2016-02-19 15:26:29.008 +0800] Backup program is starting.
INFO[2016-02-19 15:26:29.008 +0800] Scheduled backup job on 00:00
INFO[2016-02-19 15:26:29.008 +0800] Scheduled backup job on 13:00
INFO[2016-02-20 00:00:00.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-20 00:00:00.016 +0800] db
INFO[2016-02-20 00:00:10.399 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160220-00.bak.
INFO[2016-02-20 00:00:10.399 +0800] Backup finished.
INFO[2016-02-20 13:00:00.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-20 13:00:00.017 +0800] db
INFO[2016-02-20 13:00:11.872 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160220-13.bak.
INFO[2016-02-20 13:00:11.873 +0800] Backup finished.
INFO[2016-02-21 00:00:11.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-21 00:00:11.016 +0800] db
INFO[2016-02-21 00:00:22.338 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160221-00.bak.
INFO[2016-02-21 00:00:22.338 +0800] Backup finished.
INFO[2016-02-21 13:00:12.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-21 13:00:12.016 +0800] db
INFO[2016-02-21 13:00:22.767 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160221-13.bak.
INFO[2016-02-21 13:00:22.767 +0800] Backup finished.
INFO[2016-02-22 00:00:23.017 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-22 00:00:23.017 +0800] db
INFO[2016-02-22 00:00:32.908 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160222-00.bak.
INFO[2016-02-22 00:00:32.908 +0800] Backup finished.
INFO[2016-02-22 13:00:23.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-22 13:00:23.016 +0800] db
INFO[2016-02-22 13:00:34.653 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160222-13.bak.
INFO[2016-02-22 13:00:34.653 +0800] Backup finished.
INFO[2016-02-23 00:00:33.018 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-23 00:00:33.018 +0800] db
INFO[2016-02-23 00:00:43.447 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160223-00.bak.
INFO[2016-02-23 00:00:43.447 +0800] Backup finished.
INFO[2016-02-23 13:00:35.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-23 13:00:35.016 +0800] db
INFO[2016-02-23 13:00:46.023 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160223-13.bak.
INFO[2016-02-23 13:00:46.023 +0800] Backup finished.
INFO[2016-02-24 00:00:44.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-24 00:00:44.016 +0800] db
INFO[2016-02-24 00:00:53.945 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160224-00.bak.
INFO[2016-02-24 00:00:53.945 +0800] Backup finished.
INFO[2016-02-24 13:00:47.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-24 13:00:47.016 +0800] db
INFO[2016-02-24 13:00:58.153 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160224-13.bak.
INFO[2016-02-24 13:00:58.153 +0800] Backup finished.
INFO[2016-02-25 00:00:54.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-25 00:00:54.016 +0800] db
INFO[2016-02-25 00:01:05.071 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160225-00.bak.
INFO[2016-02-25 00:01:05.071 +0800] Backup finished.
INFO[2016-02-25 13:00:59.016 +0800] Below are databases owned by maddie, they will be backed up:
INFO[2016-02-25 13:00:59.016 +0800] db
INFO[2016-02-25 13:01:10.720 +0800] Database db is backed up as /home/db/database-backups/2016/02/db-20160225-13.bak.
INFO[2016-02-25 13:01:10.720 +0800] Backup finished.

As you can see, after each scheduled job being executed, the next job will be delayed by 10~12 seconds which is the execution time of the backup routine.

Is this by design? If yes, is there a way to change the behaviour?

Some commits need to be reverted.

Hi!

Thank you for merging my fork! Please keep in mind that I changed the name of some includes, due to the fact that I saw my code as something exotic.

Here are the commits that need to be reverted:
d2c27bc

Also in the README file: Search and replace claudiu to jasonlvhit

Thank you again! ๐Ÿ‘

Changing the Every(1).[second, minute, etc] interface

I was thinking about changing the single time frames interface.

So basically, it would be removing the single time frame s.Every(1).Second().do(...) (et al) methods, and adding a new interface like s.EverySecond().do(...) for single seconds, and other unit time frames. Or maybe s.EverySecondDo(...).

My motivation is that informing informing a time unit for a mandatory time window seems quite redundant.

Of course this has low priority and would make breaking changes. What do you guys think about this? Any feedback?

Run task just once

Is there a idiomatic way of scheduling a task to run just once? That is to say, create a schedule that is ran once and then cleared?

The only way I can think of is:

func scheduleOnce() {
  s := gocron.NewScheduler()
  s.Every(1).Day().At("10:30").Do(foo, s)
  <- s.Start()
}

func foo(s *gocron.Scheduler) {
  // My code
  s.Clear()
}

Which I haven't actually tested yet but I figured this would be a solution to my problem.

[question] Scheduling for Month

Do we have an opportunity to see in the future something like Every(1).Month().At("00:00")? Or is there another way to achieve it?
---
best regards!
aibk

`struct select{}` for cancelation

Why you are using boolean channel for cancellation when you can use empty struct?
https://github.com/jasonlvhit/gocron/blob/master/gocron.go#L482

I propose it because:

  • As is said in this article:
The value type of done is the empty struct because the value doesn't matter
  • And the empty struct does not consume memory (read this).
As the empty struct consumes zero bytes, it follows that it needs no padding. Thus a struct comprised of empty structs also consumes no storage.

Could not remove jobs properly

I have the below code

package main

import (
	"fmt"
	"time"

	"github.com/jasonlvhit/gocron"
)

func main() {
	funmap := make(map[string]func())
	for i := 1; i < 6; i++ {
		funmap[fmt.Sprintf("func%d", i)] = getJob(i)
	}
	gs := gocron.NewScheduler()
	gs.Every(5).Seconds().Do(funmap["func1"])
	gs.Every(5).Seconds().Do(funmap["func2"])
	gs.Every(5).Seconds().Do(funmap["func3"])
	gs.Every(5).Seconds().Do(funmap["func4"])
	gs.Every(5).Seconds().Do(funmap["func5"])
	fmt.Println(gs.Len())
	gs.Start()
	time.Sleep(time.Second * 10)
	fmt.Println("*************************")
	gs.Remove(funmap["func3"])
	gs.Remove(funmap["func5"])
	time.Sleep(time.Second * 10)
	gs.Clear()
}
func getJob(i int) func() {
	return func() {
		fmt.Println("This is func:", i)
	}
}

I want to remove jobs with their names.but It deletes first two entries.
How to solve this?

Does not respect summer/winter time changes

I have a go app running with gocron which starts tasks every day at 13:00, today the task was started at 12:00. there was end of BST yesterday and we changed our clocks by one hour. The task should be started using new time settings on server.

possibility to perform a schedule from HH:MM till HH:MM ?

Hi,

Do you know whether it would be possible to include a from HH:MM till HH:MM feature ?
Let's say I want to schedule a task every 5MIN from Monday to Friday, from 8:00 till 18:30.

maybe with a
gocron.Clear()

<- gocron.Start()

That would be a good feature if it is currently not possible.

Any idea ?

Thanks,

Panics in library code

I took a look at the code, and panics were used in multiple occasions. In some cases with no error messages even. What do you think about the error handling? It seems dangerous having a panic from within a library stop an app.

Cron job exited after undefined amount of time

Hey there,

Thanks for your great work!

I'd like to ask whether anyone runs into the same problem. I built a cron job that checks my gmail inbox every 10 seconds using Gmail API.
The code runs fine but it quits running after a while.

By monitoring, I can see sometimes it quits after 3 hours, 8 hours or sometimes just around 15 20 minutes. I monitored the memory of the process but it looks like there is no memory leak issue since it take stably around 12MB when running for 1 minute.

Please comment if you ran into this problem or you have some clue about it.

Thank you,
Vu

Jobs start at different time

Hey, I'll try and explain this as best as possible.

Say I set a task at 09:30

scheduler := gocron.NewScheduler()
scheduler.Every(1).Day().At("09:30").Do(someTask)

Say my task takes 10 minutes to complete at 09:30 and ends at 09:40, the next day, the task starts at 9:40, and repeats this, so eventually this task will be running at a completely different time if left forever.

Not sure if you've noticed this, but i thought it would be worth mentioning.

Thanks!

Chainable Events?

	gocron.Every(1).
		Monday().Wednesday().Friday().Saturday().
		At("09:31").At("09:43").At("13:31").At("17:43").At("23:17").
		Do(SendSMS)

This doesn't work. Only last one stays. I was wondering if we can make it chainable like above?

I don't understand the interval part of s.Every(1).Second()

I don't understand what's the "1" inside Every(). What does it mean? Interval of what?
s.Every(1).Second().

  1. Will it memory leak?

  2. When will it end? (i would like infinite loop til forever)

  3. Is this below correct for maintenanceFunc to be ran exactly every day at 3:30am?
    //s.Every(1).Day().At("3:30").Do(maintenanceFunc)

  4. when I put as Second, it only display in intervals of 2 (i did timestamp display using

      s := gocron.NewScheduler()
      s.Every(1).Second().Do(displayTimeStamp)
      <- s.Start()
    

1543144245
1543144247
1543144249
1543144251
1543144253

help

Roadmap

This is working fine for me.

I was wondering , since its a newish project if you are thinking about a roadmap or suggestions :

  1. Retry
  2. Audit trail / logging
  3. this could be related to just logging and log shipping, and so is normally something a user of the package would do though, but just logging using the stand log package would mean that logrus etc would automatically work with it.
  4. Config driven.
    This one is pretty useful , but curious if others would use it?
    The idea is to be able to describe the various tasks and times,. Cobra and viper would be the obvious way to do it, because it would also allow running certain tasks from the CLI, and it would be able to use the config.

Thanks in advance.

Scheduling same function to run at same time fails

Scheduling the same function to run at the same time hasn't been working for me. It works fine if I schedule two different functions at the same time, but it fails with the same function (looks like it's only running the latest declared statement instead of both?).

e.g.

gocron.Every(5).Seconds().Do(someFunc, 1)
gocron.Every(5).Seconds().Do(someFunc, 2)
<-gocron.Start()

^ I see only one instance of someFunc running with param 2 -- the latest declared is run.

Any ideas what may be going wrong here? I've also tried running them in different goroutines, but it still fails.

Is this package still maintained?

Seems like there are a lot of issues and PRs that don't get tended to. Maybe put a notice in the README at least in case it's looking for more maintainers and/or should be considered stale?

(Worth adding: it still seems like the "best" cron/scheduler library for Go so I think it'd be worth it to keep maintaining it by allowing new maintainers onboard)

Setting timezones for individual jobs

Is that possible? I'm looking at a scenario where a function may run as a scheduled task at midnight in different timezones, supplied with different parameters.

100% CPU Usage

Gocron is using 100% of one core, 100% of the time.

You don't even have to schedule anything, just calling gocron.Start() will do it.

Any thoughts?

Bad performance and huge CPU consumption

Unfortunately due to constant infinite loop (in Start() function) which constantly check jobs without any delay CPU consumption of my go app is always 100%. It is the same on my Windows laptop and on a Windows server.

That is very sad because I have to run one job every hour, another job - once a day (at a certain time). All other time my application should do nothing, but it consumes 100% CPU. That makes the library completely unusable for me.

I had to switch to my custom selector with 2 timers.
Hourly timer is simple:

hourlyTick := time.Tick(1 * time.Hour)

Daily timer is a bit complicated as I have to run it at a certain time.
The code looks like that:

// for target daily time 20:00
now := time.Now()
targetDailyTime := time.Date(now.Year(), now.Month(), now.Day(), 20, 00, now.Second(), now.Nanosecond(), now.Location())

durationTillDailyTime := targetDailyTime.Sub(time.Now())
if durationTillDailyTime < 0 {
    durationTillDailyTime += 24 * time.Hour
}

dailyTick := time.Tick(durationTillDailyTime) // wait till exact time first time

select {
case <- dailyTick:
    // do stuff here
    dailyTick = time.Tick(24 * time.Hour)
}

That would be perfect if the library would be reimplemented using selectors with timers (or any other approach) so it won't consume CPU when the jobs are not running.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.