Reliable HTTP cron and delayed-job scheduling with automatic retries and full run logs — delay.run
A lot of product features are delayed jobs with a costume on: "undo send" for 10 seconds, "remind me tomorrow", "downgrade at the end of the billing period", "delete this account in 30 days", "retry this payment in three days". Once the job is in the queue, someone will ask to cancel it — the sender who caught a typo, the customer who updated a card, the user who clicked delete and immediately regretted it.
Teams either build cancel and then deal with the races, or they refuse cancel and then deal with the support tickets. Both sides have a point.
Let's argue about it.
The case for letting them cancel
The feature is incomplete without it. "Undo send" that cannot undo is a timer, not a feature. A 30-day account deletion that cannot be stopped is a threat. People understand queues in this form even if they would never use the word "queue".
The alternative is compensation, which is harder. If you cannot cancel the "send the invoice" job, you send it and then send an apology, or a credit, or a second invoice. Cancelling a job you have not run is cheaper than reversing one you have.
Support will invent cancel anyway. Someone will go into the database, or into the provider dashboard, and delete a row. An official cancel path, with auth and an audit log, is safer than an informal one.
It forces you to model job identity. If a user can cancel "the downgrade scheduled for the 1st", you have named that job, stored it, and can see it. That is useful even when nobody cancels.
The case against
The race is real. The user clicks cancel; the worker has already started; the email is on the wire, or the charge has been created, or the row is half-updated. If you tell the user "cancelled" and then the work lands, you have lied. If you tell them "too late" they are angry, but at least you were honest. Building the honest version is more state than a boolean on the job row.
Not every job has a single user who should be allowed to cancel it. A dunning retry, a fraud review follow-up, a legal hold deletion: the "user" may not be the right person, or anyone at all. A generic cancel button on the jobs dashboard is a dangerous primitive.
Cancel becomes a second control plane. Now you need authz ("whose job is this?"), idempotent cancel (two clicks, two tabs), and an answer for jobs that already started. That is a small product. Some teams should not take it on for a nightly cleanup job that nobody should be cancelling.
A cancelled job is still in backups, logs, and the provider's "already accepted" side effects. People treat cancel as "this never happened". It often did, a little.
Where I've landed
I want cancel for jobs the user asked for, and not for jobs the system owes the business, unless an operator has a specific tool.
- User-scheduled work should be cancellable until it starts: drafts, reminders, "send later", "delete my account in 30 days", undo-send. The UI should say so, and should say when it is too late.
-
The state machine is at least
queued | running | done | cancelled | failed, with a unique key, and cancel is an update that only succeeds fromqueued. If the update affected zero rows, the user gets "this already started" and you do not pretend otherwise. - Once running, prefer compensation over fake cancel if you have to do anything: a second email, a refund, a reversal. Don't add a "cancel" that kills a worker mid-transaction.
- System jobs (billing, legal, fraud) are operator-only, with an audit log, and some of them are not cancellable at all. Put that in the runbook rather than hiding a button.
- Store the reason and the actor. Future you, reading the ledger, needs to know whether a human cancelled the downgrade or a job never ran.
Cancel is only safe if you can see the job's state, and if 'queued' and 'running' are different states. delay.run keeps a log of each HTTP job attempt, which is the kind of record you want next to a cancel action: did this already fire, or is it still waiting?
The parts I'm still unsure about
- Partial work. A job that sent 20 of 500 emails when cancelled: is "cancelled" the right word, and do you send the rest? I lean "failed, with a report", not cancelled.
- Child jobs. Cancelling "offboard this user" should probably cancel the delayed "wipe data" and not the already-sent "goodbye" email. That needs a graph, which most queues are not.
- How long to offer undo-send. Ten seconds is a UX trick. Ten minutes is a real cancel window and a real race. I don't have a default I trust for every product.
A quick experiment
List the delayed jobs in your product and mark each one "user can cancel", "operator can cancel", or "runs no matter what". If everything is in the third bucket, you probably owe a few users a button. If everything is in the first, you probably have a billing or legal job that should not be there.
Related reading
- How to build a webhook inbox that deduplicates deliveries: provider event IDs, a unique index, and a 30-line handler
- Should a failed job retry forever, stop after N attempts, or wait in a dead-letter queue for a human? Let's argue about it
- What I'd do differently designing background job payloads (now that some of them sit in a queue for months)
- Make scheduled job handlers idempotent: safe to run twice
Which delayed job in your product can a user cancel today, and have you had to explain a 'cancelled' action that still ran?
Top comments (0)