recipe v0.3.0 Recipe behaviour View Source

Intro

The Recipe module allows implementing multi-step, reversible workflows.

For example, you may wanna parse some incoming data, write to two different data stores and then push some notifications. If anything fails, you wanna rollback specific changes in different data stores. Recipe allows you to do that.

In addition, a recipe doesn’t enforce any constraint around which processes execute which step. You can assume that unless you explicitly involve other processes, all code that builds a recipe is executed by default by the calling process.

Ideal use cases are:

  • multi-step operations where you need basic transactional properties, e.g. saving data to Postgresql and Redis, rolling back the change in Postgresql if the Redis write fails
  • interaction with services that simply don’t support transactions
  • composing multiple workflows that can share steps (with the help of Kernel.defdelegate/2)
  • trace workflows execution via a correlation id

You can avoid using this library if:

  • A simple with macro will do
  • You don’t care about failure semantics and just want your operation to crash the calling process
  • Using Ecto, you can express your workflow with Ecto.Multi

Heavily inspired by the ktn_recipe module included in inaka/erlang-katana.

Core ideas

  • A workflow as a set of discreet steps
  • Each step can have a specific error handling scenario
  • Each step is a separate function that receives a state with the result of all previous steps
  • Each step should be easily testable in isolation
  • Each workflow run is identified by a correlation id

Example

The example below outlines a possible workflow where a user creates a new conversation, passing an initial message.

Each step is named in steps/0. Each step definition uses data added to the workflow state and performs a specific task.

Any error shortcuts the workflow to handle_error/3, where a specialized clause for :create_initial_message deletes the conversation if the system failes to create the initial message (therefore simulating a transaction).

defmodule StartNewConversation do
  use Recipe

  ### Public API

  def run(user_id, initial_message_text) do
    state = Recipe.initial_state
            |> Recipe.assign(:user_id, user_id)
            |> Recipe.assign(:initial_message_text, initial_message_text)

    Recipe.run(__MODULE__, state)
  end

  ### Callbacks

  def steps, do: [:validate,
                  :create_conversation,
                  :create_initial_message,
                  :broadcast_new_conversation,
                  :broadcast_new_message]

  def handle_result(state) do
    state.assigns.conversation
  end

  def handle_error(:create_initial_message, _error, state) do
    Service.Conversation.delete(state.conversation.id)
  end
  def handle_error(_step, error, _state), do: error

  ### Steps

  def validate(state) do
    text = state.assigns.initial_message_text
    if MessageValidator.valid_text?(text) do
      {:ok, state}
    else
      {:error, :empty_message_text}
    end
  end

  def create_conversation(state) do
    case Service.Conversation.create(state.assigns.user_id) do
      {:ok, conversation} ->
        {:ok, Recipe.assign(state, :conversation, conversation)}
      error ->
        error
    end
  end

  def create_initial_message(state) do
    %{user_id: user_id,
      conversation: conversation,
      initial_message_text: text} = state.assigns
    case Service.Message.create(user_id, conversation.id, text) do
      {:ok, message} ->
        {:ok, Recipe.assign(state, :initial_message, message)}
      error ->
        error
    end
  end

  def broadcast_new_conversation(state) do
    Dispatcher.broadcast("conversation-created", state.assigns.conversation)
    {:ok, state}
  end

  def broadcast_new_message(state) do
    Dispatcher.broadcast("message-created", state.assigns.initial_message)
    {:ok, state}
  end
end

Link to this section Summary

Functions

Assigns a new value in the recipe state under the specified key

Returns an empty recipe state. Useful in conjunction with Recipe.run/2

Logs a step execution (debug level)

Runs a recipe, identified by a module which implements the Recipe behaviour, allowing to specify the initial state

Callbacks

Invoked any time a step fails. Receives the name of the failed step, the error and the state

Invoked at the end of the recipe, it receives the state obtained at the last step

Lists all steps included in the recipe, e.g. [:square, :double]

Link to this section Types

Link to this type error() View Source
error() :: term
Link to this type function_name() View Source
function_name() :: atom
Link to this type log_function() View Source
log_function ::
  {module, function_name} |
  (step, t -> term)
Link to this type recipe_module() View Source
recipe_module() :: atom
Link to this type run_opts() View Source
run_opts() :: [log_steps: boolean, correlation_id: Recipe.UUID.t]
Link to this type t() View Source
t() :: %Recipe{assigns: %{}, correlation_id: nil | Recipe.UUID.t, log_function: log_function, recipe_module: module, run_opts: Recipe.run_opts}

Link to this section Functions

Link to this function assign(state, key, value) View Source
assign(t, atom, term) :: t

Assigns a new value in the recipe state under the specified key.

Keys are available for reading under the assigns key.

iex> state = Recipe.initial_state |> Recipe.assign(:user_id, 1)
iex> state.assigns.user_id
1
Link to this function initial_state() View Source
initial_state() :: t

Returns an empty recipe state. Useful in conjunction with Recipe.run/2.

Link to this function log_step(step, state) View Source
log_step(step, t) :: :ok

Logs a step execution (debug level).

This function is used by default when a recipe is run with log_steps: true and can be overridden by passing log_function: {module, function_name}. See the documentation for Recipe.run/3 for more information.

Link to this function run(recipe_module, initial_state, run_opts \\ []) View Source
run(recipe_module, t, run_opts) ::
  {:ok, Recipe.UUID.t, term} |
  {:error, term}

Runs a recipe, identified by a module which implements the Recipe behaviour, allowing to specify the initial state.

In case of a successful run, it will return a 3-element tuple {:ok, correlation_id, result}, where correlation_id is a uuid that can be used to connect this workflow with another one and result is the return value of the handle_result/1 callback.

Supports an optional third argument (a keyword list) for extra options:

  • :log_steps: when true, log (at debug level) each step with the updated state
  • :log_function: this value can either be a 2-element tuple {module_name, function_name} or a plain function; the function will receive two values, the current step name and the current state, and can be used to log the current step execution. By default the Recipe.log_step/2 function is used. See Recipe.log_function/0 as well to check its typing.
  • :correlation_id: you can override the automatically generated correlation id by passing it as an option. A uuid can be generated with Recipe.UUID.generate/0

Example

Recipe.run(Workflow, Recipe.initial_state(), log_steps: true)

Link to this section Callbacks

Link to this callback handle_error(step, error, t) View Source
handle_error(step, error, t) :: term

Invoked any time a step fails. Receives the name of the failed step, the error and the state.

Link to this callback handle_result(t) View Source
handle_result(t) :: term

Invoked at the end of the recipe, it receives the state obtained at the last step.

Lists all steps included in the recipe, e.g. [:square, :double]