Hatena::Grouperlang

檜山正幸のErlang未確認情報 RSSフィード

 | 

2009-01-14 (水)

helloをちゃんと作る (4) supervisorのスケルトン

| 09:05

とりあえずこんなか。

%% -*- coding: utf-8 -*-

%%% @author Your Name <your.mail.address>
%%% @doc Description
-module(supervisor_skelton).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).
-export([start_from_shell/0]).

-define(SUP, ?MODULE).

%% @private テスト専用
%% @spec () -> pid()
%% @doc supervisorを起動するが、直後にunlinkする.
%% supervisorプロセスのPIDを返す.
start_from_shell() ->
  {ok, Pid} = start_link(),
  unlink(Pid),
  Pid.

%%====================================================================
%% API functions
%%====================================================================

%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% where
%%   Pid = pid()
%%   Error = term()
%% @doc Starts the supervisor.
start_link() ->
  supervisor:start_link({local, ?SUP}, ?MODULE, []).

%%====================================================================
%% Supervisor callbacks
%%====================================================================

%% @private
%% @spec init(Args::term()) -> Result
%% where
%%  Result = {ok, {{RestartStrategy,MaxR,MaxT}, [ChildSpec]} }
%%         | ignore
%%         | {error, Reason}
%%  RestartStrategy = one_for_all 
%%                  | one_for_one 
%%                  | rest_for_one 
%%                  | simple_one_for_one
%%  MaxR = integer()
%%  MaxT = integer()
%%  ChildSpec = {Id,StartFunc,Restart,Shutdown,Type,Modules}
%%  Id = term()
%%  StartFunc = {M,F,A}
%%  M = atom()
%%  F = atom()
%%  A = [term()]
%%  Restart = permanent | transient | temporary
%%  Shutdown = brutal_kill | integer() | infinity
%%  Type = worker | supervisor
%%  Modules = [Module] | dynamic
%%  Module = atom()

%% @doc Whenever a supervisor is started using 
%% supervisor:start_link/[2,3], this function is called by the new process 
%% to find out about restart strategy, maximum restart frequency and child 
%% specifications.
init([]) ->
  AChild = {'AName',{'AModule',start_link,[]},
            permanent,2000,worker,['AModule']},
  {ok,{{one_for_all,0,1}, [AChild]}}.

%%====================================================================
%% Internal functions
%%====================================================================

%% The End

VoluntasVoluntas2009/02/06 12:46スケルトンステキです。

m-hiyamam-hiyama2009/02/07 16:20Voluntasさん、
お試しください :-)

 |