Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Do overrides existing cron tasks #174

Open
Tigraine opened this issue Nov 29, 2021 · 1 comment
Open

.Do overrides existing cron tasks #174

Tigraine opened this issue Nov 29, 2021 · 1 comment

Comments

@Tigraine
Copy link

Tigraine commented Nov 29, 2021

I ran into a really nasty issue with StartImmediately and .Do().
Basically the following code will behave completely un-expected:

func main() {
	scheduler := gocron.NewScheduler(time.UTC)
	scheduler.Every(15 * time.Second).Do(func() {
		log.Printf("normal cron at: %v", time.Now())
	})
	scheduler.StartAsync()

	timer := time.NewTimer(5 * time.Second)
	<-timer.C
	scheduler.StartImmediately().Do(func() {
		log.Printf("Something completely different")
	})

	blockUntilTermSignal()
}

The output is:

2021/11/29 13:34:10 normal cron at: 2021-11-29 13:34:10.541174 +0100 CET m=+0.001237376
2021/11/29 13:34:25 Something completely different
2021/11/29 13:34:30 Something completely different

So until the subsequent/unqualified .Do() call the scheduler will run the "normal" 15 second func, but once a .Do is set it replaces the existing one with the newly passed func.

The (for me) expected output should have been:

2021/11/29 13:35:10 normal cron at: 2021-11-29 13:35:10.215062 +0100 CET m=+0.000477251
2021/11/29 13:35:15 Something completely different
2021/11/29 13:35:25 normal cron at: 2021-11-29 13:35:25.220175 +0100 CET m=+15.005519585

To get to this I had to change the code to:

        scheduler.Every(1).Millisecond().LimitRunsTo(1).Do(func() {
		log.Printf("Something completely different")
	})

I can understand that this is probably a mistake on my part or that I am mis-using the libs fluent interface - but then again this code looks totally normal and when reading this I'd expect to be scheduling 2 separate jobs - not to replace the existing payload :/

Maybe some safeguards can be inserted into Do to prevent it from overriding the previous already submitted func?

@JohnRoesler
Copy link
Contributor

Hey @Tigraine this repo is no longer maintained. We're keeping an active fork over at https://github.com/go-co-op/gocron

Feel free to open an issue over there!

I'll post something here quickly: what I am seeing is that we aren't handling this properly, but the first method that needs to be called is Every() or Cron() which instantiates the job. So in this case, StartImmediately() is being called it's grabbing the current job which is your "normal cron" job and then changing the frequency at which it runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants