Skip to content

.Do overrides existing cron tasks #174

@Tigraine

Description

@Tigraine

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions