Added supervisor(s) for a gen

EDIT: Below.

Why is my supervised gen_server shutting down so quickly?

I'll give these organizational names to make it more clear the chain of command that I want in my application: First I'm starting with the "assembly_line_worker" then later I'll add the "marketing_specialist" to my supervision tree...

ceo_supervisor.erl

-module(ceo_supervisor).
-behaviour(supervisor).

-export([start_link/1]).
-export([init/1]).

start_link(State) ->
     supervisor:start_link({local,?MODULE}, ?MODULE, [State]).

init([Args]) ->
     RestartStrategy = {one_for_one, 10, 60},
     ChildSpec= {assembly_line_worker_supervisor,
          {assembly_line_worker_supervisor, start_link, [Args]},
          permanent, infinity, supervisor, [assembly_line_worker_supervisor]},
     {ok, {RestartStrategy, [ChildSpec]}}.    

assembly_line_worker_supervisor.erl

-module(assembly_line_worker_supervisor).
-behaviour(supervisor).

-export([start_link/1]).
-export([init/1]). %% Internal

start_link(State) ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, [State]).

init([Args]) ->
    RestartStrategy = {one_for_one, 10, 60},
    ChildSpec = {assembly_line_worker, {assembly_line_worker, start_link, [Args]}, permanent,     
        infinity, worker, [assembly_line_worker]},
    {ok, {RestartStrategy, [ChildSpec]}}.

assembly_line_worker.erl

-module(assembly_line_worker).
...

init([State]) ->
   process_flag(trap_exit, true),
   {ok, State}.

start_link(State) ->
    gen_server:start_link({global, ?MODULE}, ?MODULE, [State], []).

handle_cast(...,State} ->
    io:format("We're getting this message.~n",[]),
    {noreply, State};
...

What's happening is that the assembly line worker does a few bits of work, like receiving a couple of messages that are sent just after the ceo_supervisor:start_link(#innovative_ideas{}) command is called, then it shuts down. Have any idea why? I know that the gen_server is receiving a few messages because it io:format's them to the console.

Thanks!


EDIT : I'm hosting this on Windows via erlsrv.exe and I found that when I start up my program via a function like so:

start() ->
    ceo_supervisor:start_link(#innovative_ideas{}),
    assembly_line_worker:ask_for_more_pay(), %% Prints out "I want more $$$" as expected,
    ok.

...this function exiting immediately causes my supervisors / gen_servers to shut down. I would expect this because all of this is linked via supervision to the original calling process, so when that exits so should the children.

So I guess a better question would be, how can I allow my supervisors to keep running after going through all of the start up configuration? Is there an option other than wrapping all of this in an application? (Which doesn't sound too bad...)

Thanks for the probing questions! I learned more about supervisors that way.

batman


To get more information about what is happening start sasl before you start your supervisor: application:start(sasl).

Another way to debug this would be to start the worker from your erlang shell send the sequence of message that crashed the server. Btw: are you sure that you need 2 levels of supervisors?


Some immediate comments:

In ceo_supervisor:init/1 your supervisor child spec should declare transient instead of permanent .

Run erl -boot start_sasl so you have the error log when something goes bad and you can get the crash report of a crasher.

If you run this in the shell and you make any mistake, then your tree will be forcibly killed. This is because you linked to the shell and the shell crashes upon errors. So you are dragging down your tree. Try something like:

 Pid = spawn(fun() -> my_app:start() end).

so you have it split off. You can kill the app by sending an exit message to Pid .

链接地址: http://www.djcxy.com/p/38246.html

上一篇: 服务器进程超时

下一篇: 添加了一个gen的主管