Cancel and shutdown
Stop tasks and shut down the scheduler cleanly
Cancel a task
Schedule methods return a task handle. Keep the handle when you need to stop a specific task.
task := scheduler.ScheduleWithFixedDelay(func(ctx context.Context) {
println("refresh cache")
}, 30*time.Second)
task.Cancel()Cancellation is useful for feature toggles, tenant-specific jobs, temporary background work, and tests that need to stop a schedule explicitly.
Shut down the scheduler
Use Shutdown when the application is stopping.
done := scheduler.Shutdown()
<-doneShutdown returns a channel so callers can wait for the scheduler lifecycle to
finish before the process exits.
func stop(scheduler chrono.TaskScheduler) {
<-scheduler.Shutdown()
}Keep scheduler shutdown close to the rest of your application shutdown flow so scheduled work does not outlive the process that owns it.
