An Oban health check for cron and CI
Most Oban failures don't raise anything: a queue stops, a job never finishes, a worker discards jobs after its last retry. Job duration metrics miss them because the jobs haven't finished. The oban_jobs table already holds the evidence; a health check reads it on a schedule and tells you when something looks wrong.
What to watch
- Executing too long: hanging or orphaned jobs.
- Due jobs waiting: a backlog, or a queue that isn't running.
- Repeated failures: one worker failing and retrying in a burst. Guide.
- Discarded and cancelled jobs: jobs that ran out of attempts or were cancelled. Guide.
- Missing cron runs: a scheduled worker that stopped being inserted. Guide.
A quick overall view (it reads every row, so avoid running it often on a large table):
SELECT queue, state, count(*)
FROM oban_jobs
GROUP BY queue, state
ORDER BY queue, state;
Run it inside Oban, and outside it
A health check that runs as an Oban job is easy to set up, but it can't report that Oban is down, because then it doesn't run. Use both:
- An Oban cron job, in its own queue, for the everyday case.
- System cron (or CI) calling your release, which starts only the repo, not your application or Oban.
- A dead-man's-switch service pinged after every run, so a check that stops running gets noticed too.
Use exit codes so cron and CI can act without parsing text: 0 for ok, non-zero for a problem, and a separate code for "the check itself couldn't run", so a database outage isn't reported as a healthy queue.
Keep the check itself safe
- Run it as a SELECT-only database user, with a statement timeout and a lock timeout, so it can never block or slow Oban.
- Query aggregates (one row per queue or worker) and use Oban's indexes; avoid reading
argsorerrorsinto logs. - Know your retention. Oban's Pruner deletes
completed,cancelledanddiscardedjobs older thanmax_age, 60 seconds by default, so any history-based check only sees that far back. - Start in report-only mode for a week or two, count false positives, tune, then page on critical findings only.
How queue_keeper_check detects this
queue_keeper_check runs eleven checks: :orphaned, :hanging, :backlog, :stalled, :retry_storm, :discarded, :cancelled, :cron_missing (opt-in: you list the workers you expect and the longest gap you allow), :dead_workers (opt-in: a window of any positive integer number of days since a worker was last inserted; see dead workers), :persisted_jobs (opt-in: leftover nonterminal jobs compared to a worker and queue inventory you supply), and :dead_worker_candidates (opt-in: high-confidence leftover workers when inactivity and a complete code scan agree, with no leftover pending or future jobs; short history is unknown).
defmodule MyApp.QueueHealth do
use Oban.Worker, queue: :health
@impl true
def perform(_job) do
report = QueueKeeperCheck.run(MyApp.Repo)
if report.status != :ok, do: Req.post!(webhook_url(), json: QueueKeeperCheck.slack_payload(report))
:ok
end
end
From the shell or CI: mix queue_keeper.check --repo MyApp.Repo. From system cron, outside Oban:
*/5 * * * * /app/bin/my_app eval 'QueueKeeperCheck.Release.main(MyApp.Repo, otp_app: :my_app)'
Both exit 0 (ok), 1 (warning), 2 (critical) or 3 (a check could not complete, or invalid arguments). On PostgreSQL each check runs in a read-only transaction that is always rolled back, with a 5 s statement_timeout, a 1 s lock_timeout and a 30 s budget per check. The mix task and QueueKeeperCheck.Release start only your repo, never your application or Oban.
Several checks read jobs in final states (:stalled, :retry_storm with include_completed: true, :discarded, :cancelled, :cron_missing, :dead_workers without a ledger, and :dead_worker_candidates), so set the Pruner's max_age to cover their windows or shrink the windows. :dead_workers stays silent when history is shorter than the window; :dead_worker_candidates reports that as unknown. Attach the last-seen ledger instead of raising max_age for those checks. With Oban Pro it is untested; read the Oban Pro compatibility notes first.
def deps do
[
{:queue_keeper_check, "~> 0.4.1"}
]
end
queue_keeper_check is a free, MIT-licensed, read-only health check for Oban (v0.4.1, early). It only runs SELECTs on oban_jobs, never reads job args, meta or errors, and sends nothing anywhere. Tested with Oban 2.24 on PostgreSQL 17, SQLite and MySQL 8.4; not tested with Oban Pro.