Oban cron job not running
Oban's cron inserts a job when a crontab expression matches the current minute. When that stops happening, nothing fails: there is no error and no row, so the job is simply missing. The only way to notice is to look for the absence of a recent row.
How Oban cron inserts jobs
# Oban 2.24: the cron service key
config :my_app, Oban,
cron: [
crontab: [
{"0 * * * *", MyApp.Workers.HourlySync},
{"0 9 * * *", MyApp.Workers.DailyReport, timezone: "America/Chicago"}
]
]
# the older plugin form still works but is deprecated in favour of Oban.Cron
config :my_app, Oban,
plugins: [{Oban.Plugins.Cron, crontab: [{"0 * * * *", MyApp.Workers.HourlySync}]}]
- At the start of every minute, the cron process on the leader checks each entry in its time zone (
Etc/UTCunless you settimezone) and inserts the jobs that match. Other nodes skip the minute. - Missed minutes are not caught up. If no node is leader at that minute, or the insert fails because the database is unreachable, that run never happens.
- The crontab is read when Oban starts. A config change takes effect after a restart or deploy.
- Each inserted job's
metahascron: true,cron_expr,cron_nameandcron_tz, so you can tell cron rows from jobs your code inserted.
Why a cron job stops running
- No leader. Without leadership, cron (and Lifeline and staging) don't run on any node. An instance started with
peer: falseorplugins: falsenever becomes leader, andtesting: :manualor:inlinealso turns off plugins and queues. Leadership is kept in theoban_peerstable and checked every 30 seconds. - The leader has no crontab. Any node running the same Oban instance can win leadership, and only the leader inserts. If some nodes start Oban without the cron config (a different release, or runtime config that differs per role), cron runs only while one of the configured nodes happens to lead.
- The entry isn't loaded in this environment. The crontab lives in a config file that doesn't apply in production, the entry was removed, or a top-level
crontab:is combined withplugins: false, which drops it. - A time zone mix-up.
"0 9 * * *"runs at 09:00 UTC unless the entry or the cron config setstimezone, which needs a time zone database such astz. - Unique options on the worker. Cron jobs use the worker's own options. If the worker is
uniqueand a matching job already exists, the insert returns that job and no new row appears. - It was inserted, but didn't run. A new row that sits in
availableis a stalled queue, and one that ends updiscardedis a failing worker. Neither is a missing cron job.
Find the last run
The newest row per expected worker, in any state. Oban stores worker without the Elixir. prefix and timestamps in UTC without a time zone.
SELECT worker,
max(inserted_at) AS last_inserted,
timezone('utc', now()) - max(inserted_at) AS since_last,
count(*) AS rows_kept
FROM oban_jobs
WHERE worker IN ('MyApp.Workers.HourlySync', 'MyApp.Workers.DailyReport')
GROUP BY worker;
A worker with no row at all doesn't appear. Then see what happened to the newest one:
SELECT DISTINCT ON (worker)
worker, id, state, attempt, inserted_at, scheduled_at, meta ->> 'cron_expr' AS cron_expr
FROM oban_jobs
WHERE worker IN ('MyApp.Workers.HourlySync', 'MyApp.Workers.DailyReport')
ORDER BY worker, inserted_at DESC;
Everything cron inserted in the last day, per expression:
SELECT meta ->> 'cron_expr' AS cron_expr, worker,
count(*) AS inserted, max(inserted_at) AS last_inserted
FROM oban_jobs
WHERE meta ->> 'cron' = 'true'
AND inserted_at >= timezone('utc', now()) - interval '1 day'
GROUP BY 1, 2
ORDER BY last_inserted;
Oban doesn't index worker, inserted_at or meta, so these queries read the whole table. If you run Oban's Pruner, completed rows older than max_age (60 seconds by default) are gone, so an empty result can mean "pruned", not "never ran".
Check leadership
SELECT name, node, started_at, expires_at,
expires_at > timezone('utc', now()) AS current
FROM oban_peers;
No current row means no leader, so no cron. From a remote console:
Oban.Peer.get_leader()
#=> "web@10.0.0.1", or nil
# on the leader node: is the cron process configured?
Oban.config().plugins |> Enum.find(&match?({Oban.Cron, _}, &1))
# validate an expression
Oban.Cron.parse("0 9 * * *")
How queue_keeper_check detects this
:cron_missing is opt-in: list the workers you expect and the longest gap you allow, in seconds. It reads the newest inserted_at for each listed worker, in any state, and fires when that row is older than the gap or there is no row at all. Always critical. Pad the gap: an hourly job checked every minute might use 3_900.
QueueKeeperCheck.run(MyApp.Repo,
cron_missing: [expected: %{"MyApp.Workers.HourlySync" => 3_900, "MyApp.Workers.DailyReport" => 90_000}]
)
- It checks insertion only. A cron job that was inserted and then discarded is caught by
:discarded; one waiting in a queue that isn't running is caught by:stalled. A worker you no longer expect to run at all is a different problem: see dead workers. - The Pruner's
max_agemust be longer than your largest gap, for examplepruner: [max_age: {1, :day}], or every pruned worker is reported as missing. - Like the :dead_workers fallback when no ledger is attached, it has no index support: one sequential scan of
oban_jobsper run, bounded by the statement timeout. On a large table, consider your own index on(worker, inserted_at). - Run it from system cron through your release as well as from Oban, so a missing cron run is still reported when Oban itself is down.
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.