cron icon indicating copy to clipboard operation
cron copied to clipboard

Add Jitter

Open mohsenpashna opened this issue 1 year ago • 1 comments

Add some Jitter to the Entry's scheduled time. The actual jitter used will be randomly chosen within the range [0, Jitter]. The jitter will always be converted to a positive duration. Plus:

  • AddFuncWithJitter adds a func to the Cron to be run on the given schedule. The spec is parsed using the time zone of this Cron instance as the default. An opaque ID is returned that can be used to later remove it.

  • AddJobWithJitter adds a Job to the Cron to be run on the given schedule with a specified amount of jitter for each invocation. The spec is parsed using the time zone of this Cron instance as the default. An opaque ID is returned that can be used to later remove it.

  • ScheduleWithJitter adds a Job to the Cron to be run on the given schedule with a specified amount of jitter for each invocation. The job is wrapped with the configured Chain.

  • WithTimeCallback uses the provided time function callback for getting the current time.

mohsenpashna avatar Jul 08 '24 01:07 mohsenpashna

@mohsenpashna I made a fork that addresses almost all issues of this repo. https://github.com/alaingilbert/cron

This is how you could do what you want to do ->

// Jitter adds some random delay before running the job
_, _ = c.AddJob("*/5 * * * *", cron.WithJitter(time.Minute, func() {
	fmt.Println("Runs every 5min, but wait 0-1 minute before starting")
}))

alaingilbert avatar Apr 19 '25 00:04 alaingilbert