Reliable HTTP cron and delayed-job scheduling with automatic retries and full run logs — delay.run
A deploy or a maintenance window has a job-shaped problem: work may be running, work may be due, and work may become due while the workers are not there. If you kill the process, the in-flight job is at the mercy of whatever retry policy you remembered to write. If you leave the workers up, they will run against a database or an API you are mid-migrate. If you pause and later resume without thinking, every missed tick and every delayed job whose run_at has passed will start at once.
You want three verbs, and you want them in this order: stop starting new work, drain what is already running, then (when you are ready) resume at a rate you can survive.
Pause means "do not start", not "disappear"
A pause has to be something a worker can see before it reserves the next job: a flag in the database, a "paused" state on the queue, a SIGTSTP / Sidekiq "quiet", a Celery warm shutdown, a Kubernetes preStop that marks the pod unready and then waits. What it should not be is kill -9, a deleted Deployment, or a crontab you emptied and hoped you would remember to restore.
-- a single-row flag the worker checks before reserving work CREATE TABLE worker_flags ( name text PRIMARY KEY, paused boolean NOT NULL DEFAULT false, updated_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO worker_flags (name, paused) VALUES ('scheduler', false) ON CONFLICT (name) DO NOTHING; UPDATE worker_flags SET paused = true, updated_at = now() WHERE name = 'scheduler'; # at the top of the reserve loop, before FOR UPDATE SKIP LOCKED if conn.execute( "SELECT paused FROM worker_flags WHERE name = 'scheduler'" ).fetchone()[0]: time.sleep(1) continue Check the flag in the same code path for cron ticks and for delayed jobs. A "stop the crontab" pause that leaves the delay worker running will still send the 15-minute reminder in the middle of the migration.
For Sidekiq, sidekiqctl quiet (or the equivalent TSTP) stops the process from fetching new jobs and lets the current ones finish. For Kubernetes CronJobs, suspend: true stops new Jobs from being created; already-running Jobs need their own drain. For a systemd timer, systemctl stop foo.timer is the pause, and a running foo.service still needs to finish or be waited on.
Drain with a deadline
After a pause, wait until reserved / in-flight jobs complete, or until a deadline, then give up and treat the rest as failed (so they retry later, once, under the normal policy — not so they are forgotten).
-- in-flight work you still owe a finish to SELECT id, job_type, reserved_at, locked_by FROM jobs WHERE status = 'running'; A practical sequence for a deploy:
- Mark the scheduler paused (or quiet the workers).
- Stop creating new ticks: suspend CronJobs, stop timers, flip the flag.
- Watch
runningdrop to zero, with a timeout you have measured (the 95th percentile of job runtime, plus a margin, is a better number than "a couple of minutes"). - If anything is still running at the deadline, send SIGTERM, wait again, and only then SIGKILL. The row should be released or marked failed, not left as
runningforever. - Deploy or migrate.
- Resume (next section), on purpose.
Kubernetes: set terminationGracePeriodSeconds longer than the longest job you are willing to let finish, and a preStop hook that quiets the worker first. If your jobs regularly run for 20 minutes and the grace period is 30 seconds, you do not have a drain, you have a kill with extra steps.
The deploy of the worker code should wait for this sequence. The deploy of the web app can usually go ahead once the workers are quiet, as long as it does not run a migration the remaining in-flight jobs still need.
Resume without a stampede
When you unpause, everything whose run_at is in the past becomes due at once. That is correct for a single delayed "send this invoice" that waited through the window. It is not correct for "every minute" jobs that missed 40 ticks, or for 10,000 delayed emails that all became due during a two-hour outage.
Before flipping the flag back:
- Know the depth and the age of due work (count and oldest
run_at). If the number is a surprise, look at it. - For cron / recurring jobs, prefer skip-the-gap or a single catch-up run with a watermark, not "fire every missed tick now". That policy should already exist; the resume is the moment it matters.
- For delayed one-off jobs, let them run, but cap concurrency and honour provider rate limits. A token bucket on the way out is better than 10,000 parallel HTTP calls.
- Resume the worker pool gradually if you scaled it to zero: one replica, then more, not 20 at once on a cold cache.
UPDATE worker_flags SET paused = false, updated_at = now() WHERE name = 'scheduler'; If you have to keep the queue paused because the backlog is unsafe, drain it with a one-off, rate-limited consumer, or with a human-approved "run this range" job, rather than hoping a standard resume will be gentle.
The incident after the incident is a paused queue coming back and firing every missed send at once. delay.run keeps a run log for scheduled HTTP jobs, so you can see what actually started after a window — and you can pause or retry a job without inventing a drain path in a shell on the night.
Test the sequence once, in staging
Rehearse: enqueue a 30-second job, start it, begin the pause-and-drain, deploy a dummy change, resume. Confirm the in-flight job finished once, a job that became due during the pause ran once after resume, and a recurring job did not run 20 times to "catch up" unless you explicitly wanted that. Then write the three commands, in order, in the deploy runbook, with the dashboard you will watch between them.
If the only "pause" you have today is SIGKILL, you do not yet have a maintenance window for your jobs. You have a coin flip for whatever was running at the time.
Related reading
- Adding a 'send this later' delay queue to an app that only had cron? A 12-point checklist so delayed jobs survive deploys, retries and cancellations
- When the scheduler comes back after an outage, should it run every missed tick, or skip to the next one? Let's argue about it
- How to detect when a scheduled job starts taking longer than its own interval (overlapping runs) and stop it safely
- GitHub Actions scheduled workflow not running (or running late)? 8 causes and how to fix each
How do you stop jobs today for a deploy — quiet and drain, a flag, or a kill? What happened the last time you came back and the backlog hit a rate limit?
