tarearbol v0.99.4 Tarearbol.DynamicManager behaviour View Source
The scaffold implementation to dynamically manage many similar tasks running as processes.
It creates a main supervisor, managing the GenServer
holding the state and
DynamicSupervisor
handling chidren. It has a strategy :rest_for_one
,
assuming that if the process holding the state crashes, the children will be
restarted.
Typically one calls use Tarearbol.DynamicManager
and implements at least
children_specs/0
callback and receives back supervised tree with a state
and many processes controlled by DynamicSupervisor
.
To see how it works you might try
defmodule DynamicManager do
use Tarearbol.DynamicManager
def children_specs do
for i <- 1..10, do: {"foo_#{i}", []}, into: %{}
end
end
{:ok, pid} = DynamicManager.start_link()
The above would spawn 10
children with IDs "foo_1".."foo_10"
.
DynamicManager
also allows dynamic workers management. It exports three
functions
@spec put(id :: binary(), opts :: Enum.t()) :: pid()
@spec del(id :: binary()) :: :ok
@spec get(id :: binary()) :: Enum.t()
The semantics of put/2
arguments is the same as a single child_spec
,
del/1
and get/1
receive the unique ID of the child and shutdown it or
return it’s payload respectively.
Link to this section Summary
Callbacks
This function is called to retrieve the map of children with name as key and a workers as the value.
Declares an instance-wide callback to report state; if the startup process
takes a while, it’d be run in handle_continue/2
and this function will be
called after it finishes so that the application might start using it.
Declares a callback to report slow process (when the scheduler cannot process in a reasonable time).
The main function, doing all the job, supervised.
Link to this section Callbacks
This function is called to retrieve the map of children with name as key and a workers as the value.
The value must be an enumerable with keys among:
:payload
(passed as second argument toperform/2
, defaultnil
):timeout
(time between iterations ofperform/2
, default1
second):lull
(threshold to notify latency in performing, default1.1
(the threshold is:lull
times the:timeout
))
This function should not care about anything save for producing side effects.
It will be backed by DynamicSupervisor
. The value it returns will be put
into the state under children
key.
handle_state_change(state)
View Source (since 0.9.0)handle_state_change(state :: :down | :up | :starting | :unknown) :: :ok | :restart
Declares an instance-wide callback to report state; if the startup process
takes a while, it’d be run in handle_continue/2
and this function will be
called after it finishes so that the application might start using it.
If the application is not interested in receiving state updates, e. g. when all it needs from runners is a side effect, there is a default implementation that does nothing.
Declares a callback to report slow process (when the scheduler cannot process in a reasonable time).
The main function, doing all the job, supervised.
It will be called with the child id
as first argument and the
payload
option to child spec as second argument (defaulting to nil,
can also be ignored if not needed).
Return values
perform/2
might return
:halt
if it wants to be killed{:ok, result}
to store the last result and reschedule with default timeout{:replace, id, payload}
to replace the current worker with the new one{{:timeout, timeout}, result}
to store the last result and reschedule in given timeout interval- or deprecated anything else will be treated as a result