Skip to main content

src/pharos@internal@supervisor.erl

-module(pharos@internal@supervisor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pharos/internal/supervisor.gleam").
-export([start_link/6]).
-export_type([alert_manager_spec/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(false).

-type alert_manager_spec() :: {alert_manager_spec,
        pharos@alert:alert_data(),
        gleam@erlang@process:name(pharos@alert_manager:message())}.

-file("src/pharos/internal/supervisor.gleam", 138).
?DOC(false).
-spec poller_supervisor_child(pharos@config:config()) -> gleam@otp@supervision:child_specification(gleam@otp@static_supervisor:supervisor()).
poller_supervisor_child(Config) ->
    Specs = lists:append(
        pharos@internal@poller:child_specs(
            erlang:element(2, Config),
            erlang:element(14, Config)
        ),
        pharos@internal@poller:probe_child_specs(
            erlang:element(4, Config),
            erlang:element(14, Config)
        )
    ),
    Inner = gleam@list:fold(
        Specs,
        gleam@otp@static_supervisor:new(one_for_one),
        fun(Builder, Spec) -> gleam@otp@static_supervisor:add(Builder, Spec) end
    ),
    gleam@otp@static_supervisor:supervised(Inner).

-file("src/pharos/internal/supervisor.gleam", 164).
?DOC(false).
-spec state_machine_error_to_string(eparch@state_machine:start_error()) -> binary().
state_machine_error_to_string(Error) ->
    case Error of
        init_timeout ->
            <<"alert manager init timeout"/utf8>>;

        {init_failed, Reason} ->
            Reason;

        {init_exited, _} ->
            <<"alert manager init exited"/utf8>>;

        {already_started, _} ->
            <<"alert manager name already registered"/utf8>>
    end.

-file("src/pharos/internal/supervisor.gleam", 117).
?DOC(false).
-spec alert_manager_child(
    pharos@alert:alert_data(),
    pharos@event_bus:event_bus(),
    gleam@erlang@process:name(pharos@alert_manager:message())
) -> gleam@otp@supervision:child_specification(pharos@alert_manager:alert_manager()).
alert_manager_child(Data, Bus, Name) ->
    gleam@otp@supervision:worker(
        fun() -> case pharos@alert_manager:start_link(Data, Bus, Name) of
                {ok, Handle} ->
                    case gleam_erlang_ffi:process_named(Name) of
                        {ok, Pid} ->
                            {ok, {started, Pid, Handle}};

                        {error, _} ->
                            {error,
                                {init_failed,
                                    <<"alert manager registered name not found after start"/utf8>>}}
                    end;

                {error, Error} ->
                    {error, {init_failed, state_machine_error_to_string(Error)}}
            end end
    ).

-file("src/pharos/internal/supervisor.gleam", 100).
?DOC(false).
-spec alert_supervisor_child(
    gleam@erlang@process:name(pharos@alert:alert_event()),
    list(alert_manager_spec())
) -> gleam@otp@supervision:child_specification(gleam@otp@static_supervisor:supervisor()).
alert_supervisor_child(Bus_name, Manager_specs) ->
    Bus = pharos@event_bus:from_name(Bus_name),
    Inner = gleam@list:fold(
        Manager_specs,
        gleam@otp@static_supervisor:new(one_for_one),
        fun(Builder, Spec) ->
            {alert_manager_spec, Data, Name} = Spec,
            gleam@otp@static_supervisor:add(
                Builder,
                alert_manager_child(Data, Bus, Name)
            )
        end
    ),
    gleam@otp@static_supervisor:supervised(Inner).

-file("src/pharos/internal/supervisor.gleam", 157).
?DOC(false).
-spec event_manager_error_to_string(eparch@event_manager:start_error()) -> binary().
event_manager_error_to_string(Error) ->
    case Error of
        {already_started, _} ->
            <<"event bus already started"/utf8>>;

        {start_failed, Reason} ->
            Reason
    end.

-file("src/pharos/internal/supervisor.gleam", 90).
?DOC(false).
-spec event_bus_child(gleam@erlang@process:name(pharos@alert:alert_event())) -> gleam@otp@supervision:child_specification(pharos@event_bus:event_bus()).
event_bus_child(Bus_name) ->
    gleam@otp@supervision:worker(
        fun() -> case pharos@event_bus:start_link(Bus_name) of
                {ok, {Pid, Bus}} ->
                    {ok, {started, Pid, Bus}};

                {error, Error} ->
                    {error, {init_failed, event_manager_error_to_string(Error)}}
            end end
    ).

-file("src/pharos/internal/supervisor.gleam", 54).
?DOC(false).
-spec start_link(
    pharos@config:config(),
    gleam@erlang@process:name(pharos@alert:alert_event()),
    list(alert_manager_spec()),
    gleam@otp@supervision:child_specification(pharos@internal@hot_buffer:hot_buffer()),
    gleam@option:option(gleam@otp@supervision:child_specification(pharos@internal@spillover:spillover())),
    gleam@option:option(gleam@otp@supervision:child_specification(pharos@internal@connection:connection()))
) -> {ok, gleam@otp@actor:started(gleam@otp@static_supervisor:supervisor())} |
    {error, gleam@otp@actor:start_error()}.
start_link(
    Config,
    Bus_name,
    Manager_specs,
    Buffer_child,
    Spillover_child,
    Connection_child
) ->
    With_spillover = case Spillover_child of
        none ->
            _pipe = gleam@otp@static_supervisor:new(rest_for_one),
            _pipe@1 = gleam@otp@static_supervisor:add(
                _pipe,
                event_bus_child(Bus_name)
            ),
            gleam@otp@static_supervisor:add(_pipe@1, Buffer_child);

        {some, Child} ->
            _pipe@2 = gleam@otp@static_supervisor:new(rest_for_one),
            _pipe@3 = gleam@otp@static_supervisor:add(
                _pipe@2,
                event_bus_child(Bus_name)
            ),
            _pipe@4 = gleam@otp@static_supervisor:add(_pipe@3, Buffer_child),
            gleam@otp@static_supervisor:add(_pipe@4, Child)
    end,
    Base = begin
        _pipe@5 = With_spillover,
        _pipe@6 = gleam@otp@static_supervisor:add(
            _pipe@5,
            alert_supervisor_child(Bus_name, Manager_specs)
        ),
        gleam@otp@static_supervisor:add(
            _pipe@6,
            poller_supervisor_child(Config)
        )
    end,
    _pipe@7 = case Connection_child of
        none ->
            Base;

        {some, Child@1} ->
            gleam@otp@static_supervisor:add(Base, Child@1)
    end,
    gleam@otp@static_supervisor:start(_pipe@7).