Durable.DSL.Workflow (Durable v2.0.2)

View Source

Provides the workflow macro for defining durable workflows.

Usage

defmodule MyApp.OrderWorkflow do
  use Durable

  workflow "process_order", timeout: hours(2) do
    step :validate, fn order ->
      {:ok, Map.put(order, "order_id", order["id"])}
    end

    step :charge, [retry: [max_attempts: 3]], fn order ->
      {:ok, Map.put(order, "receipt", Payments.charge(order["total"]))}
    end
  end
end

A workflow's name is a non-empty string written in place, and is used once in a module: anything else is a compile error.

Options

  • :timeout - How long a run may take, in milliseconds, from when it is due to start — waits and sleeps included. A run past it fails with a "timeout" error, like any failure (see :on_failure): a step still running is stopped (and not retried), and a waiting run is woken to fail. Steps in a run with a timeout run in their own process; see the step :timeout option. 0, a negative number or :infinity means no timeout and compiles with a warning; anything else that isn't a positive integer is a compile error.
  • :on_failure - What a run does when it fails for good. :halt (default) leaves it failed with nothing undone, so Durable.retry/2 can carry it on once the cause is fixed, or Durable.rollback/2 can undo it. :rollback runs the compensate: of every completed step at once; a run rolled back can't be retried. [rollback_after: ms] halts, and rolls the run back if nobody has retried it or rolled it back by then: for a run holding something that lapses, such as a card authorization. Whichever is set, a step that returns {:rollback, reason} rolls the run back
  • :queue - The queue runs start on unless Durable.start/3 names one. A step's own :queue moves the run for that step
  • :reuse - When a start with a key: may run again after the run holding that key has finished: :after_close (default, after any finish), :after_failure (only after it failed, was cancelled or rolled back; a completed run keeps its key) or :never. See the Workflow keys guide.
  • :on_duplicate - What Durable.start/3 does when the key is taken, unless the call says otherwise: :use_existing (default, {:ok, holder_id}), :error ({:error, {:key_taken, holder_id}}), :cancel_existing (replace the running holder with a new run), :run_again (run once more after the holder ends, with the newest input) or {:send_event, name} (send the new input to the running holder as an event)
  • :debounce - [period: ms, timeout: ms]: a start with a key: waits period before it runs, and each start with the same key in that time gives it the newer input and restarts the wait, up to timeout after the first start. timeout is optional
  • :log_metadata - A function of a step's data returning a keyword list, set as Logger.metadata/1 before every attempt, on top of the :workflow, :workflow_id, :step and :attempt Durable always sets: log_metadata: fn data -> [account_id: data["account_id"]] end

Summary

Functions

Defines a workflow with the given name and options.

Functions

workflow(name, opts \\ [], list)

(macro)

Defines a workflow with the given name and options.