Current section

Files

Jump to
extreme_system lib system aggregate_group.ex
Raw

lib/system/aggregate_group.ex

defmodule Extreme.System.AggregateGroup do
use Supervisor
alias Extreme.System.AggregateSup
alias Extreme.System.PidRegistry
def start_link(aggregate_module),
do: Supervisor.start_link __MODULE__, aggregate_module, name: group_name(aggregate_module)
def spawn_aggregate(aggregate_module, key, child_params \\ []) do
{:ok, pid} = aggregate_module |> sup_name |> AggregateSup.start_child(child_params)
:ok = aggregate_module |> registry_name |> PidRegistry.register(key, pid)
{:ok, pid}
end
def get_registered(aggregate_module, key),
do: aggregate_module |> registry_name |> PidRegistry.get(key)
def init(aggregate_module) do
children = [
supervisor( AggregateSup, [aggregate_module, [name: sup_name(aggregate_module)]]),
worker( PidRegistry, [registry_name(aggregate_module)])
]
supervise children, strategy: :one_for_all
end
defp group_name(aggregate_module), do: "#{aggregate_module}Group" |> String.to_atom
defp sup_name(aggregate_module), do: "#{aggregate_module}Sup" |> String.to_atom
defp registry_name(aggregate_module), do: "#{aggregate_module}Registry" |> String.to_atom
end