Skip to content

oliver-kriska/claude-elixir-phoenix / targets/amp/skills/oban

Use when writing, scheduling, testing or debugging Oban jobs, even for a how-to question: workers, cron, retries, unique jobs, queues, Pro Workflow/Batch. Load it before touching job code; it holds the job rules.

FileKindSize
SKILL.mdskill md3.9 KB
references/oban-pro-basics.mdmarkdown9.7 KB
references/queue-config.mdmarkdown3.1 KB
references/testing-patterns.mdmarkdown2.9 KB
references/worker-patterns.mdmarkdown3.0 KB

references/testing-patterns.md

View on GitHub
# Oban Testing Patterns Reference

> **Official docs**: <https://hexdocs.pm/oban/testing.html>
> **Oban Pro testing**: <https://hexdocs.pm/oban_pro/testing.html>

## Configuration

```elixir
# config/test.exs
config :my_app, Oban, testing: :manual  # Or :inline
```

## Test Helper Setup

```elixir
defmodule MyApp.DataCase do
  using do
    quote do
      use Oban.Testing, repo: MyApp.Repo
    end
  end
end
```

## Assert Enqueued

```elixir
test "enqueues welcome email on signup" do
  {:ok, user} = Accounts.create_user(%{email: "test@example.com"})

  assert_enqueued worker: MyApp.WelcomeWorker,
                  args: %{user_id: user.id},
                  queue: :mailers
end

# Scheduled with tolerance
assert_enqueued worker: MyApp.ReminderWorker,
                scheduled_at: {tomorrow, delta: 60}

# Assert NOT enqueued
refute_enqueued worker: MyApp.WelcomeWorker
```

## Execute Jobs

```elixir
test "processes valid order" do
  assert :ok = perform_job(MyApp.OrderWorker, %{order_id: 1})
end

test "cancels for missing order" do
  assert {:cancel, _} = perform_job(MyApp.OrderWorker, %{order_id: -1})
end
```

## Drain Queues

```elixir
test "full workflow processes correctly" do
  {:ok, _} = MyApp.start_import(file_path: "data.csv")

  assert %{success: 3, failure: 0} = Oban.drain_queue(
    queue: :imports,
    with_scheduled: true,
    with_recursion: true
  )
end
```

## Oban Pro Testing

> If project uses Oban Pro, use `Oban.Pro.Testing` instead of `Oban.Testing`.
> Pro.Testing API may vary between versions — check `mix hex.docs online oban_pro`.

### Setup

```elixir
# In test helper or DataCase
use Oban.Pro.Testing, repo: MyApp.Repo
```

### Drain Jobs

```elixir
# Pro uses drain_jobs/1 instead of drain_queue/2
assert %{success: 3, failure: 0} = drain_jobs()
drain_jobs(with_scheduled: true, with_recursion: true)
```

### Test Workflows and Batches

```elixir
# Workflow test — insert and drain
test "ETL workflow completes" do
  alias Oban.Pro.Workflow

  Workflow.new()
  |> Workflow.add(:extract, ExtractWorker.new(%{source: "test"}))
  |> Workflow.add(:load, LoadWorker.new(%{}), deps: [:extract])
  |> Oban.insert_all()

  assert %{success: 2} = drain_jobs()
end
```

---

## Anti-patterns

```elixir
# ❌ No idempotency for payments
def perform(%Job{args: %{"amount" => amount}}) do
  PaymentGateway.charge(amount)  # Will double-charge on retry!
end

# ✅ Idempotency key
def perform(%Job{args: %{"amount" => amount, "idempotency_key" => key}}) do
  case Payments.find_by_key(key) do
    {:ok, existing} -> {:ok, existing}
    :not_found -> PaymentGateway.charge(amount, idempotency_key: key)
  end
end

# ❌ Large data in args
%{file_content: large_binary}

# ✅ Store reference
%{file_path: "/uploads/abc123.csv"}

# ❌ No unique constraint for user actions
# Double-click creates duplicate jobs!

# ✅ Add unique constraint
unique: [period: {5, :minutes}, keys: [:user_id, :action]]
```
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall