towerops/vendor/oban_pro/usage-rules/batches.md
Graham McIntire 6c05d45dd6 deps: upgrade oban_pro to 1.7.0
Bumps vendored Oban Pro from 1.6.13 to 1.7.0, which transitively bumps
oban core to 2.22.1 (requires migrations v14) and db_connection to 2.10.0.

- mix.exs constraint: ~> 1.6 → ~> 1.7
- vendor/oban_pro/ replaced with v1.7.0 hex package
- migration 20260430142200: Oban core v12 → v14 (must run first)
- migration 20260430142241: Oban Pro 1.7.0 schema additions

Also corrects a pre-existing constraint name in AgentAssignment that was
left stale by the equipment→devices rename migration (20260117190134).
The unique index is named agent_assignments_device_id_index, but the
schema's unique_constraint/3 still pointed at the old equipment_id name,
causing the test suite to surface Ecto.ConstraintError instead of a
changeset error. Required to get the suite green for this upgrade.
2026-04-30 10:26:18 -05:00

43 lines
1 KiB
Markdown

# Batches
Batches group jobs together and run callbacks after processing.
```elixir
defmodule MyApp.EmailWorker do
use Oban.Pro.Worker
@behaviour Oban.Pro.Batch
@impl Oban.Pro.Worker
def process(%{args: %{"email" => email}}) do
MyApp.Mailer.deliver(email)
end
@impl Oban.Pro.Batch
def batch_completed(job) do
Logger.info("Batch #{job.meta["batch_id"]} completed")
:ok
end
end
```
## Creating batches
```elixir
alias Oban.Pro.Batch
emails
|> Enum.map(&EmailWorker.new(%{email: &1}))
|> Batch.new()
|> Oban.insert_all()
```
## Callbacks
| Callback | Triggered when |
|---------------------|---------------------------------------------|
| `batch_attempted/1` | All jobs executed at least once |
| `batch_completed/1` | All jobs completed successfully |
| `batch_exhausted/1` | All jobs completed, cancelled, or discarded |
| `batch_cancelled/1` | First job is cancelled |
| `batch_discarded/1` | First job is discarded |