Packages

A toolkit for building event-driven applications in Elixir with event sourcing and CQRS patterns

Current section

Files

Jump to
ex_sorcery lib sorcery auto_start.ex
Raw

lib/sorcery/auto_start.ex

defmodule Sorcery.AutoStart do
@moduledoc """
Manages auto-starting of domains and services.
Configure auto-starting in your config:
config :sorcery, :auto_start,
domains: [
MyApp.UserDomain,
MyApp.SettingsDomain
],
services: [
MyApp.NotificationService,
MyApp.ReadModelService
]
"""
def child_spec(_opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, []},
type: :supervisor,
restart: :permanent,
shutdown: 5000
}
end
def start_link do
children = build_children()
Supervisor.start_link(children, strategy: :one_for_one, name: __MODULE__)
end
defp build_children do
domains = Application.get_env(:sorcery, :auto_start, []) |> Keyword.get(:domains, [])
services = Application.get_env(:sorcery, :auto_start, []) |> Keyword.get(:services, [])
domain_children = Enum.map(domains, fn domain ->
%{
id: {Sorcery.Domain.Server, domain},
start: {Sorcery.Domain.Server, :start_singleton, [domain]},
restart: :permanent,
type: :worker
}
end)
service_children = Enum.map(services, fn service ->
%{
id: service,
start: {service, :start_link, [[]]},
restart: :permanent,
type: :worker
}
end)
domain_children ++ service_children
end
end