Durable.DSL.Workflow (Durable v2.0.2)
View SourceProvides 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
endA 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:timeoutoption.0, a negative number or:infinitymeans 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, soDurable.retry/2can carry it on once the cause is fixed, orDurable.rollback/2can undo it.:rollbackruns thecompensate: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 unlessDurable.start/3names one. A step's own:queuemoves the run for that step:reuse- When a start with akey: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- WhatDurable.start/3does 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 akey:waitsperiodbefore it runs, and each start with the same key in that time gives it the newer input and restarts the wait, up totimeoutafter the first start.timeoutis optional:log_metadata- A function of a step's data returning a keyword list, set asLogger.metadata/1before every attempt, on top of the:workflow,:workflow_id,:stepand:attemptDurable always sets:log_metadata: fn data -> [account_id: data["account_id"]] end
Summary
Functions
Defines a workflow with the given name and options.