View Source WorkflowMetal.Executor behaviour (workflow_metal v0.2.1)

Defines an executor module.

The WorkflowMetal.Executor behaviour is used to define a transition.

supported-return-values

Supported return values

  • :started - the executor is started, and report its state latter, this is useful for long-running tasks(for example: timer).
  • {:completed, workitem_output} - the executor has been started and comploted already.
  • {:failed, reason} - the executor failed to execute.

example

Example

defmodule ExampleExecutor do
  use WorkflowMetal.Executor

  alias WorkflowMetal.Storage.Schema

  @impl WorkflowMetal.Executor
  def execute(%Schema.Workitem{}, options) do
    {:ok, _tokens} = preexecute(workitem, options)

    {:completed, {:output, :ok}}
  end
end

defmodule AsyncExampleExecutor do
  use WorkflowMetal.Executor

  alias WorkflowMetal.Storage.Schema

  @impl WorkflowMetal.Executor
  def execute(%Schema.Workitem{} = workitem, options) do
    {:ok, tokens} = preexecute(workitem, options)

    Task.async(__MODULE__, :run, [workitem, tokens, options])
    :started
  end

  def run(%Schema.Workitem{} = workitem, _tokens, _options) do
    WorkflowApplication.complete_workitem(workitem, {:output, :ok})
  end
end

Link to this section Summary

Callbacks

Abandon a workitem.

Merge outputs of all workitems.

Run an executor and return its state to the workitem process.

Link to this section Types

Specs

options() :: [
  executor_params: WorkflowMetal.Storage.Schema.Transition.executor_params(),
  application: WorkflowMetal.Application.t()
]

Specs

Specs

workitem() :: WorkflowMetal.Storage.Schema.Workitem.t()

Specs

workitem_output() :: WorkflowMetal.Storage.Schema.Workitem.output()

Link to this section Callbacks

Link to this callback

abandon(workitem, options)

View Source

Specs

abandon(workitem(), options()) :: :ok

Abandon a workitem.

You can cleanup some state at this time.

Link to this callback

build_token_payload(list, options)

View Source

Specs

build_token_payload([workitem(), ...], options()) :: {:ok, token_payload()}

Merge outputs of all workitems.

Link to this callback

execute(workitem, options)

View Source

Specs

execute(workitem(), options()) :: :started | {:completed, workitem_output()}

Run an executor and return its state to the workitem process.