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
@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 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()]}
@type template_output() :: [ Runbox.Message.t() | Runbox.Scenario.OutputAction.oa_params() | Runbox.Runtime.RuntimeInstruction.t() ]
Callbacks
@callback handle_asset_discovery(Runbox.Message.t()) :: handle_asset_discovery_resp()
@callback handle_message(Runbox.Message.t(), Runbox.Runtime.Stage.Unit.t()) :: handle_message_resp()
@callback init(integer(), Runbox.Runtime.Stage.Unit.t()) :: {:ok, template_output(), Runbox.Runtime.Stage.Unit.t()}
@callback instances() :: instances_def()
@callback subscriptions() :: [subscription_def()]
Functions
@spec handle_asset_discovery(template :: module(), Runbox.Message.t()) :: handle_asset_discovery_resp() | any()
Invokes template_module.asset_discovery
with given message
.
@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
.
@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.
@spec instances(template :: module()) :: instances_def()
Returns list of of asset units.
@spec subscriptions(template :: module()) :: [subscription_def()]
Returns subscriptions defined in template_module
.