How to Prevent Pilled Up Job Executions in GoCron if Job Takes Longer than Scheduled Interval? #784
-
I'm using the github.com/go-co-op/gocron package in my Go project to schedule tasks. The job is set to run every 2 minutes, but there's a scenario where the task might take longer than the interval (e.g., the job takes 7 minutes to complete). After the long-running job completes, multiple job instances get triggered back-to-back, which causes overlapping executions. func newScheduler(loc *time.Location) {
e := gocron.NewScheduler(loc)
e.SingletonModeAll()
e.Every("2m").Do(func() {
Logger.Warn("Task Started at: " + time.Now().Format("2006-01-02 15:04:05"))
err := tasks.MyTask()
if err != nil {
Logger.Error("Error occurred during task execution: ", err)
}
Logger.Warn("Task Completed at: " + time.Now().Format("2006-01-02 15:04:05"))
})
e.StartAsync()
select {}
} Problem: Question: What I've tried: Desired Outcome: Any guidance on how to handle this in GoCron or alternative approaches would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Looks like you're using v1. If you upgrade to V2, you can use Reschedule with singleton mode https://pkg.go.dev/github.com/go-co-op/gocron/v2#example-WithSingletonMode |
Beta Was this translation helpful? Give feedback.
Looks like you're using v1. If you upgrade to V2, you can use Reschedule with singleton mode https://pkg.go.dev/github.com/go-co-op/gocron/v2#example-WithSingletonMode