Oban discarded and cancelled jobs
A job is discarded when Oban gives up on it and cancelled when something told it to stop. Neither raises anything in your application: the row just changes state. A spike for one worker usually means a dependency or a deploy broke it and the retries are already spent.
How jobs get discarded
- Out of attempts. An attempt fails (an
{:error, reason}return, an exception, a crash, orOban.TimeoutError) whenattempthas reachedmax_attempts, 20 by default. Oban setsstate = 'discarded'anddiscarded_at, and appends the error toerrors. perform/1returned:discardor{:discard, reason}. Oban 2.24 documents both as deprecated in favour of{:cancel, reason}, but they still discard the job and record the reason, whatever attempts are left.- Lifeline rescue on the last attempt. Lifeline moves an
executingjob older thanrescue_aftertodiscardedwhenattempt >= max_attempts. It adds no error entry for that attempt. See orphaned jobs.
How jobs get cancelled
perform/1returned{:cancel, reason}. Oban setscancelledandcancelled_atand records the reason inerrors.Oban.cancel_job/1orOban.cancel_all_jobs/1. These cancelexecuting,available,scheduledorretryablejobs, killing any that are executing, and setcancelled_at. A job cancelled this way while it wasn't executing gets no new error entry, so a cancelled spike with no new errors was most likely a person or a script. Cancelling a worker's retries during a retry storm produces exactly that.
Find the spike
Discarded jobs per worker in the last hour. Oban stores timestamps in UTC without a time zone, so compare against timezone('utc', now()).
SELECT worker, queue, count(*) AS discarded, max(discarded_at) AS latest
FROM oban_jobs
WHERE state = 'discarded'
AND discarded_at >= timezone('utc', now()) - interval '1 hour'
GROUP BY worker, queue
ORDER BY discarded DESC;
Cancelled jobs, the same way. attempt is still 0 for a job that was cancelled before any queue fetched it.
SELECT worker, queue, count(*) AS cancelled,
count(*) FILTER (WHERE attempt = 0) AS never_ran
FROM oban_jobs
WHERE state = 'cancelled'
AND cancelled_at >= timezone('utc', now()) - interval '1 hour'
GROUP BY worker, queue
ORDER BY cancelled DESC;
Read why
Each entry in errors is a map with attempt, at and error; on PostgreSQL the column is a jsonb[], so the newest is errors[array_length(errors, 1)]. Grouping discarded jobs by how they ended:
SELECT worker,
attempt >= max_attempts AS out_of_attempts,
coalesce((errors[array_length(errors, 1)] ->> 'attempt')::int, 0) < attempt
AS no_error_for_last_attempt,
split_part(errors[array_length(errors, 1)] ->> 'error', E'\n', 1) AS last_error,
count(*) AS jobs
FROM oban_jobs
WHERE state = 'discarded'
AND discarded_at >= timezone('utc', now()) - interval '1 hour'
GROUP BY 1, 2, 3, 4
ORDER BY jobs DESC
LIMIT 20;
out_of_attemptstrue, an error on the last attempt. The job used up its retries andlast_erroris the final failure. If the same worker shows a retry storm, this is where the storm ends.out_of_attemptsfalse.perform/1returned:discardor{:discard, reason}; the reason is inlast_error.no_error_for_last_attempttrue. The final attempt recorded nothing, which is what Lifeline leaves when it discards an executing job with no attempts left: the job was orphaned, or ran longer thanrescue_after.
For cancelled jobs, the newest error holds the reason when perform/1 returned {:cancel, reason}. Error text can contain customer data from exception messages, so be careful where you paste it.
Retry them after the fix
import Ecto.Query
cutoff = DateTime.add(DateTime.utc_now(), -3600, :second)
from(j in Oban.Job,
where: j.worker == "MyApp.Workers.Sync" and j.state == "discarded",
where: j.discarded_at >= ^cutoff
)
|> Oban.retry_all_jobs()
Oban.retry_all_jobs/1 sets the jobs to available for immediate execution, clears discarded_at and cancelled_at, and raises max_attempts where needed so each job gets one more attempt. It never touches available or executing jobs. Retry only work that is still wanted and safe to run again: a cancelled job may have been cancelled on purpose.
Alert on them
Inside your app, Oban's telemetry covers jobs that finish through perform/1. A job that runs out of attempts emits [:oban, :job, :exception] with state: :discard:
defmodule MyApp.ObanAlerts do
require Logger
def attach do
:telemetry.attach("oban-discards", [:oban, :job, :exception], &__MODULE__.handle_event/4, nil)
end
def handle_event(_event, _measurements, %{state: :discard, job: job}, _config) do
Logger.error("Oban discarded #{job.worker} job #{job.id} after #{job.attempt} attempts")
end
def handle_event(_event, _measurements, _meta, _config), do: :ok
end
Jobs discarded by Lifeline and jobs cancelled with Oban.cancel_job/1 while they weren't executing don't emit job events, and a per-job log line doesn't tell you about a spike. Counting rows in oban_jobs on a schedule catches all of them. If you run Oban's Pruner, it deletes discarded and cancelled rows older than max_age (60 seconds by default), so any count only sees that far back.
How queue_keeper_check detects this
Two checks count jobs in final states, per worker, summed across queues:
:discardedfires when a worker hasmaxor more jobs (default 5) withdiscarded_atin the lastwindowseconds (default 3600).:cancelledfires when a worker hasmaxor more jobs (default 10) withcancelled_atin the lastwindowseconds (default 3600).
Both are warnings, critical at twice the limit. Each finding names the worker, its queues, the count and up to 10 sample job ids. :retry_storm also counts discarded and cancelled jobs on attempt 2 or later in its own, shorter window.
QueueKeeperCheck.run(MyApp.Repo,
discarded: [window: 3600, max: 5],
cancelled: [window: 3600, max: 10]
)
With the Pruner's default 60 second max_age, shrink the windows to fit (for example discarded: [window: 60]) or raise max_age, otherwise the 3600 second windows only ever see the last minute.
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.