Job Lifecycle
View SourceOban 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.
Job States
Jobs exist in one of six possible states:
available
— 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 again
Initial States
When 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 ascheduled_at
timestamp orschedule_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()
Executing State
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 it
Retry Cycle
When 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 attempt
This cycle continues until it reaches a final state.
Final States
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 attempts
Cleaning Up Jobs
Oban's Pruner only removes final state jobs (completed
, cancelled
, and discarded
). 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.