Oban.drain_queue

You're seeing just the function drain_queue, go back to Oban module for more information.
Link to this function

drain_queue(name \\ __MODULE__, opts)

View Source (since 0.4.0)

Specs

drain_queue(name(), [drain_option()]) :: drain_result()

Synchronously execute all available jobs in a queue.

All execution happens within the current process and it is guaranteed not to raise an error or exit.

Draining a queue from within the current process is especially useful for testing. Jobs that are enqueued by a process when Ecto is in sandbox mode are only visible to that process. Calling drain_queue/2 allows you to control when the jobs are executed and to wait synchronously for all jobs to complete.

Failures & Retries

Draining a queue uses the same execution mechanism as regular job dispatch. That means that any job failures or crashes are captured and result in a retry. Retries are scheduled in the future with backoff and won't be retried immediately.

By default jobs are executed in safe mode, just as they are in production. Safe mode catches any errors or exits and records the formatted error in the job's errors array. That means exceptions and crashes are not bubbled up to the calling process.

If you expect jobs to fail, would like to track failures, or need to check for specific errors you can pass the with_safety: false flag.

Scheduled Jobs

By default, drain_queue/2 will execute all currently available jobs. In order to execute scheduled jobs, you may pass the :with_scheduled flag which will cause scheduled jobs to be marked as available beforehand.

Options

  • :queue - a string or atom specifying the queue to drain, required
  • :with_recursion — whether to keep draining a queue repeatedly when jobs insert more jobs
  • :with_safety — whether to silently catch errors when draining, default true
  • :with_scheduled — whether to include any scheduled jobs when draining, default false

Example

Drain a queue with three available jobs, two of which succeed and one of which fails:

Oban.drain_queue(queue: :default)
%{success: 2, failure: 1}

Drain a queue including any scheduled jobs:

Oban.drain_queue(queue: :default, with_scheduled: true)
%{success: 1, failure: 0}

Drain a queue and assert an error is raised:

assert_raise RuntimeError, fn -> Oban.drain_queue(queue: :risky, with_safety: false) end

Drain a queue repeatedly until there aren't any more jobs to run. This is particularly useful for testing jobs that enqueue other jobs.

Oban.drain_queue(queue: :default, with_recursion: true)
%{success: 2, failure: 1}