Oban Background Jobs Reference
Quick reference for Elixir Oban patterns.
Oban Pro Detection
Before applying patterns, check for Oban Pro:
grep -E "oban_pro|oban_web" mix.exs
grep -r "use Oban.Pro.Worker" lib/
grep -r "Oban.Pro.Engines.Smart" config/
If Oban Pro detected, use Pro patterns for ALL new workers:
| Standard Oban |
Oban Pro |
use Oban.Worker |
use Oban.Pro.Worker |
def perform(%Job{}) |
def process(%Job{}) |
Oban.Testing |
Oban.Pro.Testing |
| Advisory lock engine |
Oban.Pro.Engines.Smart |
Pro features (all optional): args_schema (typed args), Workflows, Batches, Chunks,
Relay, hooks, encryption, deadlines, chaining, Smart Engine (global concurrency + rate limiting).
Pro plugins (DynamicCron, DynamicLifeline, DynamicPruner) enhance OSS equivalents — swap module, don't run both.
See references/oban-pro-basics.md for all patterns and migration guide.
Iron Laws — Never Violate These
- JOBS MUST BE IDEMPOTENT — Safe to retry. Use idempotency keys for payments
- JOBS MUST STORE IDs, NOT STRUCTS — JSON serialization.
%{user_id: 1} not %{user: %User{}}
- JOBS MUST HANDLE ALL RETURN VALUES —
:ok, {:error, _}, {:cancel, _}, {:snooze, _}
- ARGS USE STRING KEYS — Pattern match
%{"user_id" => id} not %{user_id: id}
- UNIQUE CONSTRAINTS FOR USER ACTIONS — Prevent double-click duplicates
- NEVER STORE LARGE DATA IN ARGS — Store references (IDs, paths), not content
- SMART ENGINE: NEVER USE
attempt TO LIMIT SNOOZES — Snooze rolls back attempt counter. Use meta["snoozed"] instead. Causes infinite loops
Quick Worker Template
defmodule MyApp.Workers.ExampleWorker do
use Oban.Worker,
queue: :default,
max_attempts: 5,
unique: [period: {5, :minutes}, keys: [:entity_id]]
@impl Oban.Worker
def perform(%Oban.Job{args: %{"entity_id" => id}}) do
case process(id) do
{:ok, _} -> :ok
{:error, :not_found} -> {:cancel, "Entity not found"}
{:error, :rate_limited} -> {:snooze, {5, :minutes}}
{:error, reason} -> {:error, reason}
end
end
end
Return Value Meanings
| Return |
State |
Behavior |
:ok |
completed |
Success |
{:ok, value} |
completed |
Success with value |
{:error, reason} |
retryable |
Retry with backoff |
{:cancel, reason} |
cancelled |
Stop permanently |
{:snooze, seconds} |
scheduled |
Delay and retry |
Quick Decisions
Which Queue?
- Critical operations → High concurrency (20+)
- Mailers/Webhooks (I/O) → Medium concurrency (30-50)
- CPU-intensive → Low concurrency (3-5)
- External APIs → Use
dispatch_cooldown for rate limiting
Testing Pattern
use Oban.Testing, repo: MyApp.Repo
# Assert enqueued
assert_enqueued worker: MyApp.Worker, args: %{id: 1}
# Execute and verify
assert :ok = perform_job(MyApp.Worker, %{id: 1})
Common Anti-patterns
| Wrong |
Right |
%{user_id: id} pattern match |
%{"user_id" => id} (string keys) |
%{user: %User{}} in args |
%{user_id: 1} (IDs only) |
| No idempotency for payments |
Use idempotency keys |
| Ignoring return values |
Handle all outcomes explicitly |
References
For detailed patterns, see:
references/worker-patterns.md - Worker options, backoff, timeout
references/queue-config.md - Queue design, pool sizing, cron, Smart Engine
references/testing-patterns.md - Testing, assertions, drain (OSS + Pro)
references/oban-pro-basics.md - Pro.Worker, Workflow, Batch, Chunk, Relay, plugins
1---2name: oban3description: Oban job processing — workers, perform/1 (OSS) and process/1 (Pro); Use4 when writing Oban workers, queue config…5---67# Oban Background Jobs Reference89Quick reference for Elixir Oban patterns.1011## Oban Pro Detection1213**Before applying patterns, check for Oban Pro:**1415```bash16grep -E "oban_pro|oban_web" mix.exs17grep -r "use Oban.Pro.Worker" lib/18grep -r "Oban.Pro.Engines.Smart" config/19```2021**If Oban Pro detected**, use Pro patterns for ALL new workers:2223| Standard Oban | Oban Pro |24|---------------|----------|25| `use Oban.Worker` | `use Oban.Pro.Worker` |26| `def perform(%Job{})` | `def process(%Job{})` |27| `Oban.Testing` | `Oban.Pro.Testing` |28| Advisory lock engine | `Oban.Pro.Engines.Smart` |2930**Pro features** (all optional): `args_schema` (typed args), Workflows, Batches, Chunks,31Relay, hooks, encryption, deadlines, chaining, Smart Engine (global concurrency + rate limiting).32Pro plugins (DynamicCron, DynamicLifeline, DynamicPruner) **enhance** OSS equivalents — swap module, don't run both.33See `references/oban-pro-basics.md` for all patterns and migration guide.3435---3637## Iron Laws — Never Violate These38391. **JOBS MUST BE IDEMPOTENT** — Safe to retry. Use idempotency keys for payments402. **JOBS MUST STORE IDs, NOT STRUCTS** — JSON serialization. `%{user_id: 1}` not `%{user: %User{}}`413. **JOBS MUST HANDLE ALL RETURN VALUES** — `:ok`, `{:error, _}`, `{:cancel, _}`, `{:snooze, _}`424. **ARGS USE STRING KEYS** — Pattern match `%{"user_id" => id}` not `%{user_id: id}`435. **UNIQUE CONSTRAINTS FOR USER ACTIONS** — Prevent double-click duplicates446. **NEVER STORE LARGE DATA IN ARGS** — Store references (IDs, paths), not content457. **SMART ENGINE: NEVER USE `attempt` TO LIMIT SNOOZES** — Snooze rolls back attempt counter. Use `meta["snoozed"]` instead. Causes infinite loops4647## Quick Worker Template4849```elixir50defmodule MyApp.Workers.ExampleWorker do51 use Oban.Worker,52 queue: :default,53 max_attempts: 5,54 unique: [period: {5, :minutes}, keys: [:entity_id]]5556 @impl Oban.Worker57 def perform(%Oban.Job{args: %{"entity_id" => id}}) do58 case process(id) do59 {:ok, _} -> :ok60 {:error, :not_found} -> {:cancel, "Entity not found"}61 {:error, :rate_limited} -> {:snooze, {5, :minutes}}62 {:error, reason} -> {:error, reason}63 end64 end65end66```6768## Return Value Meanings6970| Return | State | Behavior |71|--------|-------|----------|72| `:ok` | `completed` | Success |73| `{:ok, value}` | `completed` | Success with value |74| `{:error, reason}` | `retryable` | Retry with backoff |75| `{:cancel, reason}` | `cancelled` | Stop permanently |76| `{:snooze, seconds}` | `scheduled` | Delay and retry |7778## Quick Decisions7980### Which Queue?8182- **Critical operations** → High concurrency (20+)83- **Mailers/Webhooks (I/O)** → Medium concurrency (30-50)84- **CPU-intensive** → Low concurrency (3-5)85- **External APIs** → Use `dispatch_cooldown` for rate limiting8687### Testing Pattern8889```elixir90use Oban.Testing, repo: MyApp.Repo9192# Assert enqueued93assert_enqueued worker: MyApp.Worker, args: %{id: 1}9495# Execute and verify96assert :ok = perform_job(MyApp.Worker, %{id: 1})97```9899## Common Anti-patterns100101| Wrong | Right |102|-------|-------|103| `%{user_id: id}` pattern match | `%{"user_id" => id}` (string keys) |104| `%{user: %User{}}` in args | `%{user_id: 1}` (IDs only) |105| No idempotency for payments | Use idempotency keys |106| Ignoring return values | Handle all outcomes explicitly |107108## References109110For detailed patterns, see:111112- `references/worker-patterns.md` - Worker options, backoff, timeout113- `references/queue-config.md` - Queue design, pool sizing, cron, Smart Engine114- `references/testing-patterns.md` - Testing, assertions, drain (OSS + Pro)115- `references/oban-pro-basics.md` - Pro.Worker, Workflow, Batch, Chunk, Relay, plugins