Robfig cron jobs with context cancellation and jitter

6574
0

Scheduled jobs get risky when deploys overlap or when jobs take longer than their interval. I use robfig/cron with a wrapper that creates a per-run context.WithTimeout, adds a bit of jitter to avoid synchronized load across instances, and logs start/finish. The jitter matters more than people think: without it, many pods run a cron at exactly the same second, which can spike a dependency. I also prefer idempotent jobs and, when needed, a distributed lock so only one instance does the work. The code below focuses on the core: wire the cron, add a wrapper that respects cancellation, and keep run durations bounded. In production you can add metrics (success/fail/duration) and alerts on repeated failures.