cron icon indicating copy to clipboard operation
cron copied to clipboard

How to start job from the past time?

Open BugKillerPro opened this issue 2 years ago • 1 comments

       parser := cron.NewParser(
		cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
	)
	c := cron.New(cron.WithParser(parser))

	// 0 0 */5 * *
	c.AddFunc("0 0 0 */5 * ?", func() {
		fmt.Println("Every 5 day")
	})

If this program run at 2024-01-01,then fmt.Println("Every 5 day") will run at 2024-01-06、2024-01-11 ......, but if I restart this program at 2024-01-02,then fmt.Println("Every 5 day") will run at 2024-01-07、2024-01-12 ......., So ,How to avoid restart influence?or How to start job from the past time? or How to set a specific past time as original cron start time ?

BugKillerPro avatar Feb 20 '24 05:02 BugKillerPro

I have the same requirement. My current approach is to record the lastExecTime in the database, and after restarting, use schedule.Next(lastExecTime) to calculate the next accurate execution time. Then, I set up a timer to execute the specific task.

I think it would be beneficial if cron.AddJob could include a recoverTime parameter to support scenarios like this.

bysph avatar Jul 09 '25 09:07 bysph