Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Nested supervisors don't necessarily have to be registered

  • Loading branch information...
commit 0aab0da063ceeef9694bdd05707a8ac24f7a1d33 1 parent 7958cc4
@yrashk yrashk authored
View
5 include/gen_supervisor.hrl
@@ -17,6 +17,7 @@
-record(supervisor, {
id :: term(),
+ registered = false :: term(),
restart_strategy = one_for_one :: one_for_one | one_for_all | rest_for_one |
simple_one_for_one,
max_restarts = {1, 60} :: {pos_integer(), pos_integer()},
@@ -28,6 +29,7 @@
-record(one_for_one, {
id :: term(),
+ registered = false :: term(),
max_restarts = {1,60} :: {pos_integer(), pos_integer()},
children = [] :: [gen_supervisor:supervisable()],
%%
@@ -37,6 +39,7 @@
-record(one_for_all, {
id :: term(),
+ registered = false :: term(),
max_restarts = {1,60} :: {pos_integer(), pos_integer()},
children = [] :: [gen_supervisor:supervisable()],
%%
@@ -46,6 +49,7 @@
-record(rest_for_one, {
id :: term(),
+ registered = false :: term(),
max_restarts = {1,60} :: {pos_integer(), pos_integer()},
children = [] :: [gen_supervisor:supervisable()],
%%
@@ -55,6 +59,7 @@
-record(simple_one_for_one, {
id :: term(),
+ registered = false :: term(),
max_restarts = {1,60} :: {pos_integer(), pos_integer()},
children = [] :: [gen_supervisor:supervisable()],
%%
View
33 src/gen_supervisor.erl
@@ -39,10 +39,12 @@ start_link(Module, Args) ->
start_link(SupName, Module, Args) ->
supervisor:start_link(SupName, ?MODULE, {Module, Args}).
--spec start_sup(atom(), supervisor()) -> {ok, pid()}.
+-spec start_sup(atom() | undefined, supervisor()) -> {ok, pid()}.
-start_sup(Id, SupervisorSpec) ->
- supervisor:start_link({local, Id}, ?MODULE, {spec, SupervisorSpec}).
+start_sup(false, SupervisorSpec) ->
+ supervisor:start_link(?MODULE, {spec, SupervisorSpec});
+start_sup(Reg, SupervisorSpec) ->
+ supervisor:start_link({local, Reg}, ?MODULE, {spec, SupervisorSpec}).
%% Internal functions
init_result(ignore) ->
@@ -147,65 +149,70 @@ maybe_convert_supervisors([Spec|Specs]) ->
[maybe_convert_supervisor(Spec)|maybe_convert_supervisors(Specs)].
maybe_convert_supervisor(#one_for_one{
- id = Id, restart = Restart,
+ id = Id,
+ registered = Reg, restart = Restart,
shutdown = Shutdown
} = Sup) ->
#child{
id = Id,
type = supervisor,
- start_func = {?MODULE, start_sup, [Id, Sup]},
+ start_func = {?MODULE, start_sup, [Reg, Sup]},
restart = Restart,
shutdown = Shutdown,
modules = [?MODULE]
};
maybe_convert_supervisor(#one_for_all{
- id = Id, restart = Restart,
+ id = Id,
+ registered = Reg, restart = Restart,
shutdown = Shutdown
} = Sup) ->
#child{
id = Id,
type = supervisor,
- start_func = {?MODULE, start_sup, [Id, Sup]},
+ start_func = {?MODULE, start_sup, [Reg, Sup]},
restart = Restart,
shutdown = Shutdown,
modules = [?MODULE]
};
maybe_convert_supervisor(#rest_for_one{
- id = Id, restart = Restart,
+ id = Id,
+ registered = Reg, restart = Restart,
shutdown = Shutdown
} = Sup) ->
#child{
id = Id,
type = supervisor,
- start_func = {?MODULE, start_sup, [Id, Sup]},
+ start_func = {?MODULE, start_sup, [Reg, Sup]},
restart = Restart,
shutdown = Shutdown,
modules = [?MODULE]
};
maybe_convert_supervisor(#simple_one_for_one{
- id = Id, restart = Restart,
+ id = Id,
+ registered = Reg, restart = Restart,
shutdown = Shutdown
} = Sup) ->
#child{
id = Id,
type = supervisor,
- start_func = {?MODULE, start_sup, [Id, Sup]},
+ start_func = {?MODULE, start_sup, [Reg, Sup]},
restart = Restart,
shutdown = Shutdown,
modules = [?MODULE]
};
maybe_convert_supervisor(#supervisor{
- id = Id, restart = Restart,
+ id = Id,
+ registered = Reg, restart = Restart,
shutdown = Shutdown
} = Sup) ->
#child{
id = Id,
type = supervisor,
- start_func = {?MODULE, start_sup, [Id, Sup]},
+ start_func = {?MODULE, start_sup, [Reg, Sup]},
restart = Restart,
shutdown = Shutdown,
modules = [?MODULE]
View
5 test/gen_supervisor_tests.erl
@@ -2,10 +2,11 @@
-include_lib("eunit/include/eunit.hrl").
t_starts_supervisor() ->
- ?assertMatch([{some_sup, _, _, _}], supervisor:which_children(test_sup)).
+ ?assertMatch([{some_sup, _, _, _},{first, _, _, _}], supervisor:which_children(test_sup)),
+ ?assertMatch(Pid when is_pid(Pid), whereis(some_sup)).
t_nested_supervisor() ->
- [{some_sup, Pid, _, _}] = supervisor:which_children(test_sup),
+ [{some_sup, Pid, _, _},{first, _, _, _}] = supervisor:which_children(test_sup),
?assertMatch([{some, _, _, _}], supervisor:which_children(Pid)).
gen_supervisor_test_() ->
View
26 test/test_sup.erl
@@ -16,18 +16,22 @@ start_link() ->
init([]) ->
#one_for_one{
children = [
- #one_for_one{
- id = some_sup,
- children = [
- #worker{
- id = some,
- restart = permanent,
- start_func = {?MODULE, some_start_link, [some]}
- }
- ]
- }
+ #one_for_one{
+ id = first
+ },
+ #one_for_one{
+ id = some_sup,
+ registered = some_sup,
+ children = [
+ #worker{
+ id = some,
+ restart = permanent,
+ start_func = {?MODULE, some_start_link, [some]}
+ }
+ ]
+ }
]
-
+
}.
some_start_link(_Name) ->
Please sign in to comment.
Something went wrong with that request. Please try again.