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