Durable.Executor (Durable v2.0.2)

View Source

The main workflow executor.

Responsible for:

  • Starting new workflow executions
  • Executing workflow steps sequentially
  • Managing workflow state and context
  • Handling workflow suspension and resumption

Summary

Functions

Cancels every pending, running or waiting run that matches filters (those of Durable.Query.list_executions/1, without :limit and :offset), one by one as cancel_workflow/3 would. Returns how many it cancelled.

Cancels a running or pending workflow.

Starts a fresh run of the same workflow, with the original input unless :input is given. The new run records which run it re-runs.

Resumes a failed workflow at the step that failed.

What rollback_workflow/2 would run: the compensation of every step that completed, newest first, as {step, compensation} names, in {:ok, plan}. Empty when there is nothing to undo or the run's code isn't on this node, and {:error, :not_found} for an id that isn't a run.

Undoes a failed run: runs the compensate: of every step that completed, newest first, on a worker.

Starts one run per {module, input} or {module, input, opts} item, in one transaction (the caller's, if it is in one). opts applies to every item, under the item's own options. Returns one result per item, in order, each what start_workflow/3 would have returned: keys are checked item by item. If the batch's key locks can't be taken, nothing is started and every item gets {:error, reason}.

Starts a new workflow execution.

Functions

cancel_all(filters, reason \\ nil)

@spec cancel_all(
  keyword(),
  String.t() | nil
) :: {:ok, non_neg_integer()}

Cancels every pending, running or waiting run that matches filters (those of Durable.Query.list_executions/1, without :limit and :offset), one by one as cancel_workflow/3 would. Returns how many it cancelled.

cancel_workflow(workflow_id, reason \\ nil, opts \\ [])

@spec cancel_workflow(String.t(), String.t() | nil, keyword()) ::
  :ok | {:error, term()}

Cancels a running or pending workflow.

execute_workflow(workflow_id, config, lock_token \\ nil)

@spec execute_workflow(String.t(), Durable.Config.t(), String.t() | nil) ::
  {:ok, map()} | {:waiting, map()} | {:error, term()}

Executes a workflow by ID.

This is called internally by queue workers or directly for immediate execution.

A worker passes the lock_token of its claim. The claim may have been taken from it before it got here, and then the run is another worker's: nothing runs, and this returns {:error, :superseded}.

A caller with no claim (inline: true, Durable.Testing) takes one here, as a poller would, and keeps it fresh while it runs. A run a worker has claimed is that worker's, and this returns {:error, :superseded}.

rerun_workflow(workflow_id, opts \\ [])

@spec rerun_workflow(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Starts a fresh run of the same workflow, with the original input unless :input is given. The new run records which run it re-runs.

Only finished runs (completed, failed, cancelled or rolled back) can be re-run, so an operator can't accidentally double up live work.

resume_workflow(workflow_id, additional_context \\ %{}, opts \\ [])

@spec resume_workflow(String.t(), map(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Resumes a waiting workflow.

Options

  • :inline - If true, execute synchronously instead of via queue (default: false). Runs until the run finishes or parks: a retry, snooze, wait or move to a step's queue is left for the queue
  • :durable - The Durable instance name (default: Durable)
  • :engine - The engine's own markers for the step that waited. Only the engine passes this: additional_context is the user's data, and has the engine's keys dropped from it

retry_workflow(workflow_id, opts \\ [])

@spec retry_workflow(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Resumes a failed workflow at the step that failed.

Completed steps are not re-run: the run continues from its saved context, and the failed step gets its full retry budget again. A failure inside a parallel block re-queues only the children that failed; the ones that completed keep their results.

Runs that were rolled back by compensation can't be resumed — their earlier effects were undone — so use rerun_workflow/2 for those.

rollback_plan(workflow_id, opts \\ [])

@spec rollback_plan(
  String.t(),
  keyword()
) :: {:ok, [{atom(), atom()}]} | {:error, Durable.Query.Opts.reason()}

What rollback_workflow/2 would run: the compensation of every step that completed, newest first, as {step, compensation} names, in {:ok, plan}. Empty when there is nothing to undo or the run's code isn't on this node, and {:error, :not_found} for an id that isn't a run.

rollback_workflow(workflow_id, opts \\ [])

@spec rollback_workflow(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Undoes a failed run: runs the compensate: of every step that completed, newest first, on a worker.

For a run that failed and was left as it was (on_failure: :halt, the default) once it's clear it shouldn't be retried. The run ends :compensated, or :compensation_failed if an undo failed, and can't be retried after. A run with nothing to undo stays :failed.

start_many(items, opts \\ [])

@spec start_many(
  [{module(), map()} | {module(), map(), keyword()}],
  keyword()
) :: [ok: String.t() | map(), error: term()]

Starts one run per {module, input} or {module, input, opts} item, in one transaction (the caller's, if it is in one). opts applies to every item, under the item's own options. Returns one result per item, in order, each what start_workflow/3 would have returned: keys are checked item by item. If the batch's key locks can't be taken, nothing is started and every item gets {:error, reason}.

start_workflow(module, input, opts \\ [])

@spec start_workflow(module(), map(), keyword()) ::
  {:ok, String.t() | %{id: String.t(), created: boolean()}} | {:error, term()}

Starts a new workflow execution.

Options

  • :workflow - The workflow name (optional, uses default if not specified)
  • :queue - The queue to use (default: "default")
  • :priority - Priority level (default: 0)
  • :scheduled_at - Schedule for future execution
  • :key - Workflow key; see Durable.start/3
  • :on_duplicate - :use_existing or :error when the key is taken
  • :durable - The Durable instance name (default: Durable)

Returns

  • {:ok, workflow_id} on success
  • {:error, reason} on failure