Current section

Files

Jump to
ex_esdb lib ex_esdb notification_system.ex
Raw

lib/ex_esdb/notification_system.ex

defmodule ExESDB.NotificationSystem do
@moduledoc """
Supervisor for event notification and distribution components.
This supervisor manages the core event notification functionality:
- LeaderSystem: Leadership responsibilities and subscription management
- EmitterSystem: Event emission and distribution
This is a core component that runs in both single-node and cluster modes.
The leadership determination happens at the store level, not the clustering level.
"""
use Supervisor
require Logger
alias ExESDB.Themes, as: Themes
alias ExESDB.StoreNaming
@impl true
def init(opts) do
Logger.info("[NOTIFICATION_SYSTEM] Initializing NotificationSystem supervisor with opts: #{inspect(opts)}")
Logger.info("[NOTIFICATION_SYSTEM] NotificationSystem PID: #{inspect(self())}, Node: #{inspect(node())}")
# Extract store_id for logging
store_id = StoreNaming.extract_store_id(opts)
Logger.info("[NOTIFICATION_SYSTEM] Store ID: #{inspect(store_id)}")
Logger.info("[NOTIFICATION_SYSTEM] Starting notification and leadership components...")
children = [
# LeaderSystem handles leadership responsibilities
{ExESDB.LeaderSystem, opts},
# EmitterSystem handles event distribution
{ExESDB.EmitterSystem, opts}
]
Logger.info("[NOTIFICATION_SYSTEM] Configured #{length(children)} notification subsystems:")
children |> Enum.with_index(1) |> Enum.each(fn {{module, _opts}, index} ->
Logger.info("[NOTIFICATION_SYSTEM] #{index}. #{inspect(module)} - #{get_component_description(module)}")
end)
Logger.info("[NOTIFICATION_SYSTEM] Using :rest_for_one strategy - EmitterSystem depends on LeaderSystem")
Logger.info("[NOTIFICATION_SYSTEM] Max restarts: 5, Max seconds: 30")
IO.puts("#{Themes.notification_system(self(), "is UP")}")
# Use :rest_for_one because EmitterSystem depends on LeaderSystem
result = Supervisor.init(children,
strategy: :rest_for_one,
max_restarts: 5,
max_seconds: 30
)
Logger.info("[NOTIFICATION_SYSTEM] NotificationSystem supervisor initialization complete")
result
end
defp get_component_description(ExESDB.LeaderSystem), do: "Leadership responsibilities and subscription management"
defp get_component_description(ExESDB.EmitterSystem), do: "Event emission and distribution"
defp get_component_description(_), do: "Notification component"
def start_link(opts) do
store_id = StoreNaming.extract_store_id(opts)
name = StoreNaming.genserver_name(__MODULE__, store_id)
Supervisor.start_link(__MODULE__, opts, name: name)
end
def child_spec(opts) do
store_id = StoreNaming.extract_store_id(opts)
%{
id: StoreNaming.child_spec_id(__MODULE__, store_id),
start: {__MODULE__, :start_link, [opts]},
restart: :permanent,
shutdown: :infinity,
type: :supervisor
}
end
end