QueueKeeper

Home / Guides

Oban jobs stuck in executing

A job enters executing when a queue fetches it and sets attempted_at. It leaves when perform/1 returns or the job is rescued. If it stays there, it is usually one of two things: it is still running (slow, blocked, or looping), or the node that ran it went away and nothing moved it back.

Why jobs get stuck

  • No timeout. A worker's default timeout/1 is :infinity. An HTTP call without a receive timeout, a lock wait or an infinite loop keeps the job in executing for as long as the process lives.
  • Orphaned by a shutdown or crash. On shutdown a queue waits shutdown_grace_period (15 seconds by default) for running jobs. A job still running after that, or on a node that was killed, stays executing in the database with no process behind it.
  • Every slot busy. A few long jobs can hold all of a queue's slots, so other due jobs wait behind them. See backed up or stalled queues.

Find them

Executing jobs older than five minutes, per queue and worker. Oban stores timestamps in UTC without a time zone, so compare against timezone('utc', now()).

SELECT queue, worker, count(*) AS jobs,
       min(attempted_at) AS oldest_start,
       timezone('utc', now()) - min(attempted_at) AS longest
FROM oban_jobs
WHERE state = 'executing'
  AND attempted_at < timezone('utc', now()) - interval '5 minutes'
GROUP BY queue, worker
ORDER BY longest DESC;

Then look at individual jobs. attempted_by holds the node name and the producer id of the queue that fetched the job.

SELECT id, queue, worker, attempt, max_attempts, attempted_at,
       attempted_by[1] AS node
FROM oban_jobs
WHERE state = 'executing'
  AND attempted_at < timezone('utc', now()) - interval '5 minutes'
ORDER BY attempted_at
LIMIT 50;

Still running, or orphaned?

The table can't tell you; a live node can. If the node in attempted_by[1] no longer exists (an old deploy, a killed pod), the job is orphaned. If the node is up, ask its queue which jobs it is running:

# in a remote console on that node
Oban.check_queue(queue: :media)
#=> %{queue: "media", limit: 10, paused: false, running: [8812, 8815], ...}

If the stuck job's id is in running, it is genuinely running. If the queue isn't running on that node, check_queue/1 returns nil.

Fix orphaned jobs: Lifeline

Oban's Lifeline plugin moves executing jobs whose attempted_at is older than rescue_after back to available, or to discarded if attempt >= max_attempts. The default rescue_after is 1 hour, checked once a minute.

# Oban 2.24: the lifeline service key
config :my_app, Oban,
  lifeline: [rescue_after: {30, :minutes}]

# or as a plugin, which older Oban 2.x versions also accept
config :my_app, Oban,
  plugins: [{Oban.Plugins.Lifeline, rescue_after: :timer.minutes(30)}]

Rescue is purely time based. Set rescue_after above your longest legitimate runtime, or a job that is still running can be started a second time. A job rescued on its last attempt is discarded with no error recorded for that attempt. Note that Oban.retry_job/1 ignores executing jobs, so it won't unstick one.

Fix hanging jobs: a worker timeout

A hanging job is still running, just for longer than it should. Give the worker a timeout so a stuck call can't hold a queue slot forever:

defmodule MyApp.Workers.Thumbnail do
  use Oban.Worker, queue: :media, max_attempts: 5

  @impl Oban.Worker
  def timeout(_job), do: :timer.minutes(2)

  @impl Oban.Worker
  def perform(%Oban.Job{args: %{"id" => id}}) do
    MyApp.Media.thumbnail(id)
  end
end

When the timeout passes, the job process exits with Oban.TimeoutError and the attempt counts as a failure, so it is retried with backoff until it runs out of attempts. To stop one job right now, Oban.cancel_job(id) kills it if it is executing and marks it cancelled.

How queue_keeper_check detects this

Two checks look at executing jobs:

  • :hanging fires when a job has been executing longer than its worker's budget: default_budget 300 seconds, with per-worker overrides in budgets. Warning, critical at twice the budget.
  • :orphaned fires when a job has been executing longer than after, default 3600 seconds (Lifeline's default rescue_after). Always critical. It measures execution age only: it cannot tell a job orphaned by a crash from one that is genuinely still running. With Lifeline on, it mostly sees orphans before Lifeline rescues them.
QueueKeeperCheck.run(MyApp.Repo,
  hanging: [default_budget: 120, budgets: %{"MyApp.Workers.Thumbnail" => 600}],
  orphaned: [after: 3600]
)

Each finding names the queue and worker, the number of jobs, the longest age, and up to 10 sample job ids.

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.

Documentation on HexDocs · Source on GitHub · Hex package

Related guides