View Source Runbox.Scenario.Template.StageBased behaviour (runbox v13.0.3)

Module defines behaviour used in scenarios as template.

Scenario template implementations are main components of scenario. Runtime uses callbacks of this behaviour to start scenario in run and to handle incoming messages. Each template behaviour implementation is represented as separate process in runtime called TemplateCarrier.

Template module name structure must be always prefixed by the module name structure of the Manifest. E.g. if you have manifest module Scenarios.MyScenario a template can be named Scenarios.MyScenario.Templates.Car or just Scenarios.MyScenario.Car but not Scenarios.MyScenarioCar.

Template can also be defined in the same module as the Manifest. This is useful in cases where there is only a single template in the scenario.

Template is intended to be used to model behaviour of assets of single type per template. Individual asset states are represented as Runbox.Runtime.Stage.Unit.t/0. Initial assets are defined via Runbox.Scenario.Template.StageBased.instances/0 callback. This callback should return list of unit definitions (Runbox.Scenario.Template.StageBased.instances_def/0).

Templates are linked together via subscriptions provided by callback Runbox.Scenario.Template.StageBased.subscriptions/0. Subscription is defined as tuple containing component definition and routing rule list. It is possible to subscribe to other templates, input and load topics. Routing rules are used to locate units when message is handled. When routing rule succeeds, callback Runbox.Scenario.Template.StageBased.handle_message/2 is called, when routing rule fails to locat existing unit, Runbox.Scenario.Template.StageBased.handle_asset_discovery/1 gets called instead.

Unit initialization can produce side effects via Runbox.Scenario.Template.StageBased.init/2 callback. This callback is triggered when instances are produced by Runbox.Scenario.Template.StageBased.instances/0 or Runbox.Scenario.Template.StageBased.handle_asset_discovery/1 callbacks. Units are already registered in the time when this callback is triggered, so changes in unit attributes won't change unit registration (defined by Runbox.Scenario.Template.StageBased.subscriptions/0).

When message from subscribed publisher is handled by the TemplateCarrier, runtime tries to locate asset unit using given subscription configuration. If there is such a unit, runtime calls Runbox.Scenario.Template.StageBased.handle_message/2 callback and uses message and located message as attributes. In case that no unit was found, Runbox.Scenario.Template.StageBased.handle_asset_discovery/1 gets called instead.

Summary

Functions

Invokes template_module.asset_discovery with given message.

Invokes template_module.handle_message with given message and unit.

Updates unit and sets initial unit state.

Returns list of of asset units.

Returns subscriptions defined in template_module.

Types

Link to this type

handle_asset_discovery_resp()

View Source
@type handle_asset_discovery_resp() ::
  {:reply, template_output()}
  | {:reply, template_output(), Runbox.Runtime.Stage.Unit.t()}
@type handle_message_resp() ::
  {:reply, template_output(), Runbox.Runtime.Stage.Unit.t()}
  | {:stop, template_output(), Runbox.Runtime.Stage.Unit.t()}
@type instances_def() :: [{:unit, id :: String.t(), attributes :: map()}]
Link to this type

routing_key_definition()

View Source
@type routing_key_definition() ::
  {message_type :: String.t() | atom(),
   {function :: := | :in, [path_to_msg_body_key :: [String.t() | atom()]],
    [path_to_attribute_key :: [String.t() | atom()]]}}
@type subscription_def() ::
  {:input_topic, topic_name :: String.t(), [routing_key_definition()]}
  | {:load_topic, topic_name :: String.t(), [routing_key_definition()]}
  | {:template, template_name :: module(), [routing_key_definition()]}

Callbacks

Link to this callback

handle_asset_discovery(t)

View Source (optional)
@callback handle_asset_discovery(Runbox.Message.t()) :: handle_asset_discovery_resp()
Link to this callback

init(integer, t)

View Source (optional)
@callback instances() :: instances_def()
@callback subscriptions() :: [subscription_def()]

Functions

Link to this function

handle_asset_discovery(template_module, msg)

View Source
@spec handle_asset_discovery(template :: module(), Runbox.Message.t()) ::
  handle_asset_discovery_resp() | any()

Invokes template_module.asset_discovery with given message.

Link to this function

handle_message(template_module, msg, unit)

View Source
@spec handle_message(
  template :: module(),
  Runbox.Message.t(),
  Runbox.Runtime.Stage.Unit.t()
) ::
  handle_message_resp() | any()

Invokes template_module.handle_message with given message and unit.

Link to this function

init(template_module, timestamp, unit)

View Source
@spec init(
  template :: module(),
  timestamp :: integer(),
  Runbox.Runtime.Stage.Unit.t()
) ::
  {:ok, template_output(), Runbox.Runtime.Stage.Unit.t()} | any()

Updates unit and sets initial unit state.

Link to this function

instances(template_module)

View Source
@spec instances(template :: module()) :: instances_def()

Returns list of of asset units.

Link to this function

subscriptions(template_module)

View Source
@spec subscriptions(template :: module()) :: [subscription_def()]

Returns subscriptions defined in template_module.