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

The function multiple starts immediately after add into cron #106

Closed
austinov opened this issue May 29, 2017 · 3 comments · Fixed by stashed/stash#139
Closed

The function multiple starts immediately after add into cron #106

austinov opened this issue May 29, 2017 · 3 comments · Fixed by stashed/stash#139

Comments

@austinov
Copy link

I'm trying to add function into CRON to running every minute:
c.AddFunc("*/1 * * * *", func() { log.Println("Run every minute") })

If I do it immediately after cron object was created, then code works as expected - the function will be executed every minute, starting at the beginning of the next minute.
But if I will add the function in a minute after the current (or later - after 2, 3 and more minutes), it begins to execute immediately after you add into the cron.
In addition, it executed as many times as the minutes passed between the creation of the cron and function was added.
For example, if you try to run the following code:

package main

import (
        "log"
        "time"
        "gopkg.in/robfig/cron.v2"
)

func main() {
        c := cron.New()
        c.Start()
        log.Println("Start CRON.")
        time.Sleep(2 * time.Minute)
        log.Println("Add function into CRON.")
        c.AddFunc("*/1 * * * *", func() { log.Println("Run every minute") })
        time.Sleep(5 * time.Second)
        c.Stop()
}

So, the function will call exactly 2 times immediately after adding into cron, because it took 2 minutes between the start of the cron (12:31:36) and adding the function into it (12:33:36).

2017/05/29 12:31:36 Start CRON.
2017/05/29 12:33:36 Add function into CRON.
2017/05/29 12:33:36 Run every minute
2017/05/29 12:33:36 Run every minute

If between execute c.Start() and c.AddFunc took 3 minutes, then we would see in the logs three calls Run every minute.
In my opinion, the first time the function should be executed at the beginning of the next minute - 12:34:00 and repeat every minute.

@austinov
Copy link
Author

austinov commented May 29, 2017

Excuse me, it was fixed in master, but not in gopkg.in/robfig/cron.v2.
Would you fix it in gopkg.in/robfig/cron.v2?

@austinov austinov reopened this May 29, 2017
tamalsaha added a commit to appscode/cron that referenced this issue Jul 17, 2017
tamalsaha added a commit to stashed/stash that referenced this issue Jul 19, 2017
tamalsaha added a commit to stashed/stash that referenced this issue Jul 19, 2017
tamalsaha added a commit to appscode/g2 that referenced this issue Jul 19, 2017
@robbbsen
Copy link

For me this is a downgrade of functionality. Think about I would like to update all of my jobs. I would've picked something like this:

var cronGlobal *cron.Cron

func refresh() {
	newCron := cron.New()
	// I would've started the new one first to ensure no interval gets missed
	newCron.Start()
	
	// Then I would've stopped the old one
	cronGlobal.Stop()
	
	// I could've added the jobs afterwards
	for _, job := range dbEntries {
		newCron.Add(job.Expression, job)
	}
	
	// Then I would've overwritten the global one
	cronGlobal = newCron
}

This way I could've been sure no interval gets missed while I'm setting up the new list. Because some of the items that are getting added to the new cron object are old ones from the previous cron object.

I hope it's clear what I'm talking about. Hope to find some other safe way.

@robfig
Copy link
Owner

robfig commented Jun 15, 2019

I agree that it is a bug. v3 branch has this fix, and it has other fixes too. I recommend upgrading to it.

@robfig robfig closed this as completed Jun 15, 2019
tamalsaha added a commit to stashed/apimachinery that referenced this issue Feb 16, 2020
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

Successfully merging a pull request may close this issue.

3 participants