application

id:cooldaemon:20070717 で作った supervisor を application から操作できるようにしてみる。

.app ファイルを準備

test と言う名前の application にするので、test.app を作成する。
内容は下記の通り。

{application, test,
 [{description, "test application"},
  {vsn, "1"},
  {modules, [test_app, sup, counter, code_lock]},
  {registered, [sup, counter, code_lock]},
  {applications, [kernel, stdlib, sasl]},
  {mod, {test_app, ["testcode"]}},
  {start_phases, []}
]}.
パラメータ 説明
description application:loaded_applications(). や application:which_applications(). で出力される説明。他に意味あるのかな?
vsn 上記と、ほぼ同じ。他に使い方あるのかな?
modules この application で使用されるモジュールを列挙。今回は、id:cooldaemon:20070717 で作った supervisor tree 配下のモジュール。
registered プロセスとして regist される名前を列挙。
applications そのうち詳細を調べて書く。
mod application:start(app_name).を実行した時に呼ばれるモジュール名と、app_name:start() のパラメータ。
start_phases そのうち詳細を調べて書く。

application の callback module を準備

-module(test_app).
-behaviour(application).

-export([start/2, stop/1]).

start(_Type, Args) ->
  io:fwrite("test start!!~n"),
  [Code | _Other] = Args,
  sup:start_link(Code).

stop(_State) -> ok.

忘れずにコンパイルする。
supervisor を修正できるなら start(_Type, Args) -> sup:start_link(Args). の方が良いなぁ。

使ってみる

起動 application:start(test).
停止 application:stop(test). でも良いけど、init:stop(). で elang shell ごと終了しても良い。