View Source Toolbox.Workflow.Instance (toolbox v5.4.10)

Struct holding the current state of a single workflow instance.

The struct should be built only via Toolbox.Workflow.new_instance/7. An updated instance is subsequently returned from every call to Toolbox.Workflow.handle_message/3.

Summary

Types

t()

Workflow instance.

Types

@type t() :: %Toolbox.Workflow.Instance{
  history: [map()],
  id: String.t(),
  last_update: integer(),
  next_possible_transition_timestamp: integer() | nil,
  possible_transitions: [map()],
  state: map(),
  status: :none | Toolbox.Workflow.status(),
  terminated?: boolean(),
  type: String.t()
}

Workflow instance.

  • status - the current status. Note that the :none status may be set only during internal initialization of the instance. AfterToolbox.Workflow.new_instance/7 returns, it will never have this status.
  • state - the current context. May be any map. Updated using the then callbacks.
  • type: an arbitrary “type”, possibly an asset type. Does not influence anything. Set using Toolbox.Workflow.new_instance/7, does not change afterwards.
  • id: an arbitrary “id”, possibly an asset id. May be used to distinguish instances. Does not influence anything. Set using Toolbox.Workflow.new_instance/7, does not change afterwards.
  • history: a list of history entries, sorted from oldest to newest. Each entry is by default of the form %{"status" => some_status, "timestamp" => some_ts}. Even the initial status is included. In case of a timeout transition, the timestamp is the timestamp of the relevant message (i.e., may be later than the actual timeout). The shape of history entries may be modified using the update_history_entry callbacks.
  • last_updated: the timestamp of the last transition. Including the transition to the initial state. In case of a timeout transition, the timestamp corresponds to the moment when the timeout fires.
  • possible_transitions: a list of transitions than can be performed from the current status. Each is by default of the form %{"status" => some_status, "timestamp" => some_ts}. The timestamp is -1 if there is no timeout bound with the transition, otherwise it is timestamp of the timeout. If a transition uses the builtin when conditions on state, they are taken into account and only if they hold, the transition is considered as possible. The shape of the possible transitions may be modified using the update_possible_transition callbacks.
  • next_possible_transition_timestamp: the earliest timestamp out of the possible transitions that specify a timeout (i.e., their timeout is not -1), or nil if there is no such transition.
  • terminated?: a flag meaning that the instance cannot move to any other status, i.e., its status is terminal and there are no transition from it.

Example

This example uses the CdPlayerWorkflow from Toolbox.Workflow moduledoc.

%Toolbox.Workflow.Instance{
  id: "Sony D-50",
  type: "cd_player",
  state: %{cd: %{album: "Zorya", artist: "Floex"}},
  last_update: 300,
  status: "playing",
  history: [
    %{"status" => "empty", "timestamp" => 100},
    %{"status" => "idle", "timestamp" => 200},
    %{"status" => "playing", "timestamp" => 300}
  ],
  possible_transitions: [
    %{"status" => "idle", "timestamp" => -1},
    %{"status" => "idle", "timestamp" => 4440300},
    %{"status" => "empty", "timestamp" => -1}
  ],
  next_possible_transition_timestamp: 4440300,
  terminated?: false
}