Jido.Exec.Chain (Jido v1.2.0)

View Source

Provides functionality to chain multiple Jido Execs together with interruption support.

This module allows for sequential execution of actions, where the output of one action becomes the input for the next action in the chain. Execution can be interrupted between actions using an interruption check function.

Examples

iex> interrupt_check = fn -> System.monotonic_time(:millisecond) > @deadline end
iex> Jido.Exec.Chain.chain([AddOne, MultiplyByTwo], %{value: 5}, interrupt_check: interrupt_check)
{:ok, %{value: 12}}

# When interrupted:
iex> Jido.Exec.Chain.chain([AddOne, MultiplyByTwo], %{value: 5}, interrupt_check: fn -> true end)
{:interrupted, %{value: 6}}

Summary

Functions

Executes a chain of actions sequentially with optional interruption support.

Types

chain_action()

@type chain_action() :: module() | {module(), keyword()}

chain_result()

@type chain_result() ::
  {:ok, map()} | {:error, Jido.Error.t()} | {:interrupted, map()} | Task.t()

interrupt_check()

@type interrupt_check() :: (-> boolean())

Functions

chain(actions, initial_params \\ %{}, opts \\ [])

@spec chain([chain_action()], map(), keyword()) :: chain_result()

Executes a chain of actions sequentially with optional interruption support.

Parameters

  • actions: A list of actions to be executed in order. Each action can be a module (the action module) or a tuple of {action_module, options}.
  • initial_params: A map of initial parameters to be passed to the first action.
  • opts: Additional options for the chain execution.

Options

  • :async - When set to true, the chain will be executed asynchronously (default: false).
  • :context - A map of context data to be passed to each action.
  • :interrupt_check - A function that returns boolean, called between actions to check if chain should be interrupted.

Returns

  • {:ok, result} where result is the final output of the chain.
  • {:error, error} if any action in the chain fails.
  • {:interrupted, result} if the chain was interrupted, containing the last successful result.
  • Task.t() if the :async option is set to true.