Current section

Files

Jump to
glixir src glixir@supervisor.erl
Raw

src/glixir@supervisor.erl

-module(glixir@supervisor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/glixir/supervisor.gleam").
-export([start_dynamic_supervisor_named/1, start_dynamic_supervisor_simple/0, start_dynamic_child/2, terminate_dynamic_child/2, which_dynamic_children/1, count_dynamic_children/1, child_spec/4, child_spec_with_restart/5, delete_child/2, restart_child/2, terminate_child/2]).
-export_type([supervisor/0, restart_strategy/0, child_type/0, supervisor_error/0, child_operation_error/0, delete_child_result/0, restart_child_result/0, terminate_child_result/0, child_counts/0, child_status/0, child_modules/0, child_info_result/0, simple_child_spec/0, dynamic_supervisor_result/0, dynamic_child_result/0, dynamic_terminate_result/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(
" Dynamic supervisor support for runtime process management\n"
" \n"
" This module provides a Gleam-friendly interface to Elixir's DynamicSupervisor,\n"
" using our Elixir helper module for clean data conversion.\n"
).
-opaque supervisor() :: {supervisor, gleam@erlang@process:pid_()}.
-type restart_strategy() :: permanent | temporary | transient.
-type child_type() :: worker | supervisor_child.
-type supervisor_error() :: {start_error, binary()} |
{child_start_error, binary(), binary()} |
{child_not_found, binary()} |
{already_started, binary()} |
{invalid_child_spec, binary()}.
-type child_operation_error() :: running |
restarting |
not_found |
simple_one_for_one |
{other_error, gleam@dynamic:dynamic_()}.
-type delete_child_result() :: delete_child_ok |
{delete_child_error, child_operation_error()}.
-type restart_child_result() :: {restart_child_ok, gleam@erlang@process:pid_()} |
{restart_child_ok_already_started, gleam@erlang@process:pid_()} |
{restart_child_error, child_operation_error()}.
-type terminate_child_result() :: terminate_child_ok |
{terminate_child_error, child_operation_error()}.
-type child_counts() :: {child_counts,
integer(),
integer(),
integer(),
integer()}.
-type child_status() :: {child_pid, gleam@erlang@process:pid_()} |
child_restarting |
child_undefined.
-type child_modules() :: modules_dynamic |
{modules_list, list(gleam@erlang@atom:atom_())} |
modules_empty.
-type child_info_result() :: {child_info,
gleam@dynamic:dynamic_(),
child_status(),
child_type(),
child_modules()}.
-type simple_child_spec() :: {simple_child_spec,
binary(),
gleam@erlang@atom:atom_(),
gleam@erlang@atom:atom_(),
list(gleam@dynamic:dynamic_()),
restart_strategy(),
integer(),
child_type()}.
-type dynamic_supervisor_result() :: {dynamic_supervisor_ok,
gleam@erlang@process:pid_()} |
{dynamic_supervisor_error, gleam@dynamic:dynamic_()}.
-type dynamic_child_result() :: {dynamic_start_child_ok,
gleam@erlang@process:pid_()} |
{dynamic_start_child_error, gleam@dynamic:dynamic_()}.
-type dynamic_terminate_result() :: dynamic_terminate_child_ok |
{dynamic_terminate_child_error, gleam@dynamic:dynamic_()}.
-file("src/glixir/supervisor.gleam", 175).
-spec restart_to_string(restart_strategy()) -> binary().
restart_to_string(Restart) ->
case Restart of
permanent ->
<<"permanent"/utf8>>;
temporary ->
<<"temporary"/utf8>>;
transient ->
<<"transient"/utf8>>
end.
-file("src/glixir/supervisor.gleam", 184).
-spec child_type_to_string(child_type()) -> binary().
child_type_to_string(Child_type) ->
case Child_type of
worker ->
<<"worker"/utf8>>;
supervisor_child ->
<<"supervisor"/utf8>>
end.
-file("src/glixir/supervisor.gleam", 192).
-spec spec_to_map(simple_child_spec()) -> gleam@dynamic:dynamic_().
spec_to_map(Spec) ->
gleam@dynamic:properties(
[{gleam_stdlib:identity(<<"id"/utf8>>),
gleam_stdlib:identity(erlang:element(2, Spec))},
{gleam_stdlib:identity(<<"start_module"/utf8>>),
gleam_stdlib:identity(
erlang:atom_to_binary(erlang:element(3, Spec))
)},
{gleam_stdlib:identity(<<"start_function"/utf8>>),
gleam_stdlib:identity(
erlang:atom_to_binary(erlang:element(4, Spec))
)},
{gleam_stdlib:identity(<<"start_args"/utf8>>),
erlang:list_to_tuple(erlang:element(5, Spec))},
{gleam_stdlib:identity(<<"restart"/utf8>>),
gleam_stdlib:identity(
restart_to_string(erlang:element(6, Spec))
)},
{gleam_stdlib:identity(<<"shutdown"/utf8>>),
gleam_stdlib:identity(
erlang:integer_to_binary(erlang:element(7, Spec))
)},
{gleam_stdlib:identity(<<"type"/utf8>>),
gleam_stdlib:identity(
child_type_to_string(erlang:element(8, Spec))
)}]
).
-file("src/glixir/supervisor.gleam", 224).
?DOC(" Start a named dynamic supervisor\n").
-spec start_dynamic_supervisor_named(binary()) -> {ok, supervisor()} |
{error, supervisor_error()}.
start_dynamic_supervisor_named(Name) ->
case 'Elixir.Glixir.Supervisor':start_dynamic_supervisor_named(Name) of
{dynamic_supervisor_ok, Pid} ->
{ok, {supervisor, Pid}};
{dynamic_supervisor_error, Reason} ->
{error, {start_error, gleam@string:inspect(Reason)}}
end.
-file("src/glixir/supervisor.gleam", 234).
?DOC(" Start a simple dynamic supervisor with defaults\n").
-spec start_dynamic_supervisor_simple() -> {ok, supervisor()} |
{error, supervisor_error()}.
start_dynamic_supervisor_simple() ->
case 'Elixir.Glixir.Supervisor':start_dynamic_supervisor_simple() of
{dynamic_supervisor_ok, Pid} ->
{ok, {supervisor, Pid}};
{dynamic_supervisor_error, Reason} ->
{error, {start_error, gleam@string:inspect(Reason)}}
end.
-file("src/glixir/supervisor.gleam", 242).
?DOC(" Start a child in a DynamicSupervisor\n").
-spec start_dynamic_child(supervisor(), simple_child_spec()) -> {ok,
gleam@erlang@process:pid_()} |
{error, binary()}.
start_dynamic_child(Supervisor, Spec) ->
{supervisor, Supervisor_pid} = Supervisor,
Spec_map = spec_to_map(Spec),
case 'Elixir.Glixir.Supervisor':start_dynamic_child(
Supervisor_pid,
Spec_map
) of
{dynamic_start_child_ok, Pid} ->
{ok, Pid};
{dynamic_start_child_error, Reason} ->
{error, gleam@string:inspect(Reason)}
end.
-file("src/glixir/supervisor.gleam", 256).
?DOC(" Terminate a dynamic child process\n").
-spec terminate_dynamic_child(supervisor(), gleam@erlang@process:pid_()) -> {ok,
nil} |
{error, binary()}.
terminate_dynamic_child(Supervisor, Child_pid) ->
{supervisor, Supervisor_pid} = Supervisor,
case 'Elixir.Glixir.Supervisor':terminate_dynamic_child(
Supervisor_pid,
Child_pid
) of
dynamic_terminate_child_ok ->
{ok, nil};
{dynamic_terminate_child_error, Reason} ->
{error, gleam@string:inspect(Reason)}
end.
-file("src/glixir/supervisor.gleam", 269).
?DOC(" Get information about all dynamic children\n").
-spec which_dynamic_children(supervisor()) -> list(child_info_result()).
which_dynamic_children(Supervisor) ->
{supervisor, Pid} = Supervisor,
'Elixir.Glixir.Supervisor':which_dynamic_children(Pid).
-file("src/glixir/supervisor.gleam", 275).
?DOC(" Count dynamic children by type and status\n").
-spec count_dynamic_children(supervisor()) -> child_counts().
count_dynamic_children(Supervisor) ->
{supervisor, Pid} = Supervisor,
'Elixir.Glixir.Supervisor':count_dynamic_children(Pid).
-file("src/glixir/supervisor.gleam", 285).
?DOC(" Create a simple child specification - the easy way to define children\n").
-spec child_spec(binary(), binary(), binary(), list(gleam@dynamic:dynamic_())) -> simple_child_spec().
child_spec(Id, Module, Function, Args) ->
{simple_child_spec,
Id,
erlang:binary_to_atom(Module),
erlang:binary_to_atom(Function),
Args,
permanent,
5000,
worker}.
-file("src/glixir/supervisor.gleam", 303).
?DOC(" Create a child spec with custom restart strategy\n").
-spec child_spec_with_restart(
binary(),
binary(),
binary(),
list(gleam@dynamic:dynamic_()),
restart_strategy()
) -> simple_child_spec().
child_spec_with_restart(Id, Module, Function, Args, Restart) ->
{simple_child_spec,
Id,
erlang:binary_to_atom(Module),
erlang:binary_to_atom(Function),
Args,
Restart,
5000,
worker}.
-file("src/glixir/supervisor.gleam", 326).
?DOC(" Delete a child specification from the supervisor\n").
-spec delete_child(supervisor(), binary()) -> delete_child_result().
delete_child(Supervisor, Child_id) ->
{supervisor, Pid} = Supervisor,
'Elixir.Glixir.Supervisor':delete_child(
Pid,
gleam_stdlib:identity(Child_id)
).
-file("src/glixir/supervisor.gleam", 335).
?DOC(" Restart a child process\n").
-spec restart_child(supervisor(), binary()) -> restart_child_result().
restart_child(Supervisor, Child_id) ->
{supervisor, Pid} = Supervisor,
'Elixir.Glixir.Supervisor':restart_child(
Pid,
gleam_stdlib:identity(Child_id)
).
-file("src/glixir/supervisor.gleam", 344).
?DOC(" Terminate a child process\n").
-spec terminate_child(supervisor(), binary()) -> terminate_child_result().
terminate_child(Supervisor, Child_id) ->
{supervisor, Pid} = Supervisor,
'Elixir.Glixir.Supervisor':terminate_child(
Pid,
gleam_stdlib:identity(Child_id)
).