Erlang OTP supervisor

I'm working on Exercise 12-2 from the book Erlang Programming. I have a module db_server_otp that implements an OTP gen_server behavior. As a stand-alone module, I have tested it and it works as expected. I now have to add a supervisor for it. Based on the example in the chapter, I created a module db_server_sup as follows:

-module(db_server_sup).
-export([start/0,init/1]).
-behavior(supervisor).

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

init(_Arguments) ->
    DbServerOtp = {db_server_otp,                %% Id
                   {db_server_otp, start, []},   %% child process
                   permanent,                    %% restart 
                   30000,                        %% shutdown (ms)
                   worker,                       %% type
                   {db_server_otp}},             %% required modules
    {ok, 
     {{one_for_all,           %% terminate all children and restart
       5,                     %% max of n restarts in MaxSeconds
       3600},                 %% MaxSeconds (s)
      [DbServerOtp]}}.        %% child process list

Both modules are located in the same directory, and I compiled both modules with .beam files are in the same working directory where I start the erlang shell. With the erlang shell, however, I'm unable to start the supervisor.

Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:8:2] [rq:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4  (abort with ^G)
1> c(db_server_otp).
./db_server_otp.erl:5: Warning: undefined callback function code_change/3 (behaviour 'gen_server')
./db_server_otp.erl:5: Warning: undefined callback function handle_info/2 (behaviour 'gen_server')
{ok,db_server_otp}
2> c(db_server_sup).
{ok,db_server_sup}
3> db_server_sup:start().
** exception exit: {start_spec,{invalid_modules,{db_server_otp}}}

Do I have to import the db_server_otp module? If so, what functions must I import? I am exporting all of my OTP methods in db_server_otp :

-module(db_server_otp).
-export([start/0,stop/0]).
-export([write/2,read/1,delete/1,match/1]).
-export([init/1,terminate/2,handle_cast/2,handle_call/3]).
-behavior(gen_server).

The 'required modules' is supposed to be a list, not a tuple.

Try using:

DbServerOtp = {db_server_otp,                %% Id
               {db_server_otp, start, []},   %% child process
               permanent,                    %% restart 
               30000,                        %% shutdown (ms)
               worker,                       %% type
               [db_server_otp]},             %% required modules

A stylistic convention: seeing the start functions in db_server_sup and db_server_otp both create links you should call them start_link . It is a common convention.

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

上一篇: erlang管理员重启策略

下一篇: Erlang OTP主管