Sig.NodeContext (Sig v0.1.0)

Copy Markdown View Source

The data a node receives when it runs.

It is passed to Sig.Node.run/1 (and as the second argument of Sig.Node.run/2).

Fields

  • :params - the deep merge of workflow, execution and node params, in that order (node params win). This is what nodes usually read.
  • :context - the deep merge of workflow and execution context.
  • :results - the results of the nodes that already completed in this execution, keyed by node name.
  • :node_name - the name of the node being run.
  • :attempt - the current attempt number, starting at 1.
  • :workflow_id / :execution_id - IDs of the workflow and execution.
  • :workflow_params, :execution_params, :node_params - each level of params before merging.
  • :workflow_context, :execution_context - each level of context before merging.

All maps come from JSONB columns, so their keys are strings.

Access

The struct implements Access, so context[:results] and context["results"] work as well as context.results.

Example

def run(%Sig.NodeContext{} = context) do
  user = context.results["fetch_user"]
  amount = context.params["amount"]
  {:ok, %{"debited" => amount, "user_id" => user["id"], "attempt" => context.attempt}}
end

Summary

Types

t()

The context passed to a node.

Functions

Builds a NodeContext, computing the merged :params and :context.

Types

t()

@type t() :: %Sig.NodeContext{
  attempt: non_neg_integer(),
  context: map(),
  execution_context: map(),
  execution_id: String.t() | nil,
  execution_params: map(),
  node_name: String.t(),
  node_params: map(),
  params: map(),
  results: map(),
  workflow_context: map(),
  workflow_id: String.t() | nil,
  workflow_params: map()
}

The context passed to a node.

Functions

build(attrs)

@spec build(map()) :: t()

Builds a NodeContext, computing the merged :params and :context.

Accepts a map with any of the struct fields except :params and :context, which are derived. Missing maps default to %{} and :attempt defaults to 1.

Examples

iex> ctx = Sig.NodeContext.build(%{
...>   workflow_params: %{"currency" => "BRL", "amount" => 10},
...>   execution_params: %{"amount" => 20},
...>   node_params: %{"amount" => 30}
...> })
iex> ctx.params
%{"currency" => "BRL", "amount" => 30}