guides/learning/job_lifecycle.md
Oban jobs follow a state machine that governs their lifecycle. Each job transitions through distinct states from the moment it's inserted until it reaches completion or another terminal state.
Jobs exist in one of eight possible states:
suspended — Jobs that are held and won't be processed until they are resumedavailable — Jobs ready to be executedscheduled — Jobs waiting for a specific time to become available for executionexecuting — Jobs currently runningretryable — Jobs that failed but will be automatically retriedcompleted — Jobs that finished successfullycancelled — Jobs that were purposefully stoppeddiscarded — Jobs that exhausted all attempts and won't be retried againWhen you first insert a job into Oban, it enters one of two initial states:
available - The default state for new jobs that should be executed immediatelyscheduled - When you provide a scheduled_at timestamp or schedule_in delay# Job inserted as "available"
%{id: 123} |> MyApp.Worker.new() |> Oban.insert()
# Job inserted as "scheduled"
%{id: 123} |> MyApp.Worker.new(schedule_in: 60) |> Oban.insert()
When a job becomes available, it waits for a queue with available capacity to claim it.
available → executing - The job is claimed by a queue with available capacity and execution
beginsscheduled → available → executing - When the scheduled time arrives, the job becomes
available and a queue may claim itWhen a job fails but hasn't reached its max_attempts limit, it automatically schedules a retry.
The retry cycle follows these steps:
executing → retryable - The job is scheduled to run after a backoff periodretryable → available → executing - The backoff period elapsed and the job can be picked up
for another attemptThis cycle continues until it reaches a final state.
After execution, a job will transition to one of these final states:
executing → completed - The job executed successfullyexecuting → cancelled - The job returned {:cancel, reason} or was manually cancelledexecuting → discarded - The job failed and reached its maximum retry attemptsCleaning Up Jobs {: .tip}
Oban's Pruner only removes final state jobs (
completed,cancelled, anddiscarded). This prevents your database from growing indefinitely while still providing visibility into recently finished jobs.
Understanding the job lifecycle helps you build more resilient systems by properly handling failure cases, monitoring job progress, and designing appropriate retry strategies for your specific workloads.