Dependencies And Parallel Work

View Source

Overview

A Flow graph gets dependencies from result references and explicit needs entries. Source order does not create a dependency.

Run the cells from top to bottom.

Install The Package

Mix.install([{:jido_action, "~> 3.0.0-beta.11"}])

Define A Diamond

defmodule FlowDependencies.Echo do
  use Jido.Action, name: "flow_dependencies_echo"

  @impl true
  def run(params, _context), do: {:ok, params}
end

defmodule FlowDependencies.Diamond do
  use Jido.Flow, name: "flow_dependencies_diamond"

  flow do
    step "left",
      action: FlowDependencies.Echo,
      params: %{side: :left, value: input(:value)}

    step "right",
      action: FlowDependencies.Echo,
      params: %{side: :right, value: input(:value)}

    step "join",
      action: FlowDependencies.Echo,
      params: %{
        left: result("left", :value),
        right: result("right", :value)
      }

    output(%{joined: result("join")})
  end
end

left and right are independent. join depends on both because its parameter expression reads both results.

{:ok, dependencies} =
  FlowDependencies.Diamond.flow()
  |> Jido.Flow.dependencies()

dependencies

Use Needs For Control Only

needs: orders work without copying predecessor output into the next Action.

step "publish",
  action: FlowDependencies.Echo,
  params: %{status: :published},
  needs: ["audit", "approve"]

Use a result reference when the Action needs data. Use needs only when the dependency is an author control decision.

Jido keeps the three forms separate:

%{
  needs: ["audit"],
  references: ["render"],
  effective: ["audit", "render"]
}

Execute A Ready Wave

{:ok, execution} =
  Jido.Exec.start(FlowDependencies.Diamond, %{value: 4})

Jido.Exec.ready(execution)

The ready values are small Jido.Exec.Work descriptions. Independent authored nodes can be in the same ready set. Run one current set with wave/1.

{:ok, _runnables, execution} = Jido.Exec.wave(execution)
{:ok, execution} = Jido.Exec.continue(execution)
{:ok, %{joined: %{left: 4, right: 4}}} = Jido.Exec.result(execution)

Ready work runs concurrently when max_concurrency is greater than 1. max_concurrency bounds the execution and defaults to 8. Use 1 for serial execution. The option does not change the graph.