Packages
commanded
1.4.11
1.4.11
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/application/supervisor.ex
defmodule Commanded.Application.Supervisor do
@moduledoc false
use Supervisor
alias Commanded.Application.Config
@doc """
Retrieves the compile time configuration.
"""
def compile_config(application, opts) do
otp_app = Keyword.fetch!(opts, :otp_app)
env_opts = Application.get_env(otp_app, application, [])
config = Keyword.merge(opts, env_opts)
{otp_app, config}
end
@doc """
Retrieves the runtime configuration.
"""
def runtime_config(application, otp_app, config, opts) do
config =
config
|> Keyword.merge(opts)
|> Keyword.put(:otp_app, otp_app)
|> Keyword.put(:application, application)
case application_init(application, config) do
{:ok, config} -> {:ok, config}
:ignore -> :ignore
end
end
@doc """
Starts the application supervisor.
"""
def start_link(application, otp_app, config, name, opts) do
Supervisor.start_link(
__MODULE__,
{application, otp_app, config, name, opts},
name: name
)
end
def init({application, otp_app, config, name, opts}) do
case runtime_config(application, otp_app, config, opts) do
{:ok, config} ->
{event_store_child_spec, config} = event_store_child_spec(name, config)
{pubsub_child_spec, config} = pubsub_child_spec(name, config)
{registry_child_spec, config} = registry_child_spec(name, config)
:ok = Config.associate(self(), name, config)
children =
event_store_child_spec ++
pubsub_child_spec ++
registry_child_spec ++
app_child_spec(name, config)
Supervisor.init(children, strategy: :one_for_one)
:ignore ->
:ignore
end
end
defp app_child_spec(name, config) do
task_dispatcher_name = Module.concat([name, Commanded.Commands.TaskDispatcher])
aggregates_supervisor_name = Module.concat([name, Commanded.Aggregates.Supervisor])
subscriptions_name = Module.concat([name, Commanded.Subscriptions])
registry_name = Module.concat([name, Commanded.Subscriptions.Registry])
snapshotting = Keyword.get(config, :snapshotting, %{})
hibernate_after = Keyword.get(config, :hibernate_after, :infinity)
[
{Task.Supervisor, name: task_dispatcher_name, hibernate_after: hibernate_after},
{Commanded.Aggregates.Supervisor,
name: aggregates_supervisor_name,
application: name,
snapshotting: snapshotting,
hibernate_after: hibernate_after},
{Commanded.Subscriptions.Registry,
application: name, name: registry_name, hibernate_after: hibernate_after},
{Commanded.Subscriptions,
application: name, name: subscriptions_name, hibernate_after: hibernate_after}
]
end
defp event_store_child_spec(name, config) do
{adapter, adapter_config} =
Commanded.EventStore.adapter(name, Keyword.get(config, :event_store))
{:ok, child_spec, adapter_meta} = adapter.child_spec(name, adapter_config)
config = Keyword.put(config, :event_store, {adapter, adapter_meta})
{child_spec, config}
end
defp pubsub_child_spec(name, config) do
{adapter, adapter_config} =
Commanded.PubSub.adapter(name, Keyword.get(config, :pubsub, :local))
{:ok, child_spec, adapter_meta} = adapter.child_spec(name, adapter_config)
config = Keyword.put(config, :pubsub, {adapter, adapter_meta})
{child_spec, config}
end
defp registry_child_spec(name, config) do
{adapter, adapter_config} =
Commanded.Registration.adapter(name, Keyword.get(config, :registry, :local))
{:ok, child_spec, adapter_meta} = adapter.child_spec(name, adapter_config)
config = Keyword.put(config, :registry, {adapter, adapter_meta})
{child_spec, config}
end
defp application_init(application, config) do
if Code.ensure_loaded?(application) and function_exported?(application, :init, 1) do
application.init(config)
else
{:ok, config}
end
end
end