Oban. Pro. Chain
(Oban Pro v1.8.6)
Chains link jobs together to ensure they run in a strict sequential order. Downstream jobs in the
chain won't execute until the upstream job is completed, cancelled, or discarded, regardless
of scheduling, snoozing, retries, or queue concurrency.
Chaining is an option for any Oban.Pro.Worker. This module only contains documentation, there
isn't anything to use or a different callback to implement.
Defining a Chain
Chains are appropriate in situations where jobs are used to synchronize internal state with outside state via events. For example, imagine a system that relies on webhooks from a payment processor to track account balance:
defmodule MyApp.WebhookWorker do
use Oban.Pro.Worker, queue: :webhooks, chain: [by: :worker]
@impl true
def process(%Job{args: %{"account_id" => account_id, "event" => event}}) do
account_id
|> MyApp.Account.find()
|> MyApp.Account.handle_webhook_event(event)
end
endNow imagine that it's essential that jobs for an account are processed in order, while jobs from
separate accounts can run concurrently. Modify the :by option to partition by :account_id:
defmodule MyApp.WebhookWorker do
use Oban.Pro.Worker, queue: :webhooks, chain: [by: [args: :account_id]]
...Webhooks for each account are guaranteed to run in order regardless of queue concurrency or errors.
Chain Partitioning
By default, chains are sequenced by worker, which means any job with the same worker forms a
chain. This approach may not always be suitable. For instance, you may want to link workers
based on a field like :account_id instead of just the worker. In such cases, you can use the
:by option to customize how chains are partitioned.
Here are a few examples of using :by to achieve fine-grained control over chain partitioning:
# Chain by :worker
use Oban.Pro.Worker, chain: [by: :worker]
# Chain by a single args key without considering the worker
use Oban.Pro.Worker, chain: [by: [args: :account_id]]
# Chain by multiple args keys without considering the worker
use Oban.Pro.Worker, chain: [by: [args: [:account_id, :cohort]]]
# Chain by worker and a single args key
use Oban.Pro.Worker, chain: [by: [:worker, args: :account_id]]
# Chain by worker and multiple args key
use Oban.Pro.Worker, chain: [by: [:worker, args: [:account_id, :cohort]]]
# Chain by a single meta key
use Oban.Pro.Worker, chain: [by: [meta: :source_id]]Chaining always includes the queue field for performance and correctness. This means that
specifying by: :worker is equivalent to by: [:queue, :worker], and by: [args: :account_id]
is equivalent to by: [:queue, args: :account_id]. Jobs in different queues will never be part
of the same chain, ensuring better performance and preventing unexpected dependencies across
queues.
Execution Order
Jobs in a chain run in the order they were inserted. The first job in a chain is inserted as
usual, while any job inserted behind an unfinished job is suspended until the job ahead of it
finishes. Only one job per chain is ever runnable, which is why ordering holds no matter how much
concurrency the queue has or how many nodes are running it.
A few details about how that interacts with the rest of a job's lifecycle:
Retries and snoozes — a job that is
retryableor snoozed hasn't finished, so everything behind it stays suspended. The chain resumes once the job completes or exhausts its attempts.Scheduling — a scheduled job waits for both the upstream job and its own
scheduled_attime. If the upstream job finishes first, the job staysscheduleduntil that time arrives.Concurrency — separate chains are independent and run concurrently, up to the queue's limits.
Handling Cancelled/Discarded
The way a chain behaves when jobs are cancelled or discarded is customizable with the
:on_discarded and :on_cancelled options.
There are two strategies for handling upstream discards and cancellations:
:ignore— keep processing jobs in the chain as if upstreamcancelledordiscardedjobs completed successfully. This is the default behaviour.:hold— stop processing any jobs in the chain until thecancelledordiscardedjob iscompletedor eventually deleted.
Here's an example of a chain that holds on discarded or cancelled:
use Oban.Pro.Worker, chain: [on_discarded: :hold, on_cancelled: :hold]Releasing a Held Chain
A held chain stays suspended until something is done about the job that's holding it. There are two ways to get it moving again:
Retry the job, e.g. with
Oban.retry_job/2or from the Web dashboard. The chain continues once it completes.Delete the job.
Oban.Pro.Lifelineperiodically repairs chains and will release the next job once the one holding it is gone.
Chains and Workflows
Using chained jobs from a workflow isn't allowed. Chains and workflows share an "on hold" scheduling mechanism which prevents predictable ordering.
Testing
Individual chain jobs are testable with perform_job/3, and run_chain/2 executes a full chain
within the test process. See Testing Chains for examples.