Question

when implementing a supervisor.. If, in the supervisor module, I do something like

init([_]) ->
{ok,
{{one_for_one, 5, 60},
[{reverese, {reverse, start_reverse, []}, permanent, brutal_kill, worker,[]}]}}.

and the reverse function is:

start_reverse() ->
Pid=spawn(?MODULE,reverse,[]).

It will not work since the start_reverse function exits normally in every case. However, when I add a line like this:

start_reverse() ->
Pid=spawn(?MODULE,reverse,[]),
{ok,Pid}.

It works, even when the function exits normally as well. Can someone explain why?

Was it helpful?

Solution

Actually.. It isnt really required for the supervisor child process to be a gen_server. The supervisor documentation specifically mentions that

The start function must create and link to the child process, and should return {ok,Child} or {ok,Child,Info} where Child is the pid of the child process and Info an arbitrary term which is ignored by the supervisor.

which is the reason why when you returned {ok, Pid} it worked..

OTHER TIPS

Easily,

The problem is that the supervisor needs a specific calling convention to support the shutdown and initialization of processes. Your code with a low-level spawn ignores that convention. You should either

  • Use a gen_something behaviour, gen_server is most common.
  • Spawn the process using proc_lib
  • Use a supervisor_bridge

Otherwise, your code will not take advantage of OTP at all. And you want it to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top