Runbox.Scenario.Helper (runbox v7.0.1)

Support for helper processes for scenarios.

Provides a supervisor and process registry under which scenarios can start their side-kick processes intended for housekeeping tasks.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Starts a supervisor with dynamic supervisor and process registry.

Returns a :via name to be used in the helper process registry.

Executes a given callback over running helper.

Link to this section Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec start_link(any()) :: Supervisor.on_start()

Starts a supervisor with dynamic supervisor and process registry.

Link to this function

via_registry(name)

@spec via_registry(term()) :: {:via, Registry, term()}

Returns a :via name to be used in the helper process registry.

Link to this function

with_helper(name, child_spec, callback)

@spec with_helper(any(), child_spec, (pid() -> any())) :: any()
when child_spec: :supervisor.child_spec() | {module(), term()} | module()

Executes a given callback over running helper.

Checks whether the helper with name registered under helper registry is already running. If not, then it starts the process according to child_spec under the dynamic supervisor. In either case, it passes the helper process pid to the callback.

It is the responsibility of the helper process to register its name with the helper registry via the via_registry/1.

examples

Examples

iex> child_spec = %{
...>   id: MyHelper,
...>   start: {
...>     Agent,
...>     :start_link,
...>     [fn -> 1 end, [name: Helper.via_registry(MyHelper)]]
...>   }
...> }
iex> :ok = Helper.with_helper(MyHelper, child_spec, fn pid -> Agent.update(pid, & &1 + 1) end)
iex> Helper.with_helper(MyHelper, child_spec, fn pid -> Agent.get(pid, & &1) end)
2