私はErlangプログラミングの本の演習12-2に取り組んでいます。OTPgen_serverの動作を実装するモジュールdb_server_otpがあります。スタンドアロンモジュールとして、私はそれをテストしました、そしてそれは期待通りに動作します。そのためのスーパーバイザーを追加する必要があります。この章の例に基づいて、次のようにモジュールdb_server_supを作成しました。
-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
両方のモジュールは同じディレクトリにあり、.beamファイルを使用して両方のモジュールをコンパイルしました。erlangシェルを起動するのと同じ作業ディレクトリにあります。しかし、erlangシェルではスーパーバイザーを起動できません。
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}}}
db_server_otpモジュールをインポートする必要がありますか?もしそうなら、どの関数をインポートする必要がありますか?私はすべてのOTPメソッドを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).