PageRenderTime 81ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/support/templates/mochiwebapp_skel/src/mochiapp_sup.erl

http://github.com/basho/mochiweb
Erlang | 56 lines | 33 code | 12 blank | 11 comment | 3 complexity | c948ca2b19a7c7cea0e008245adcabd4 MD5 | raw file
 1%% @author {{author}}
 2%% @copyright {{year}} {{author}}
 3
 4%% @doc Supervisor for the {{appid}} application.
 5
 6-module({{appid}}_sup).
 7-author("{{author}}").
 8
 9-behaviour(supervisor).
10
11%% External exports
12-export([start_link/0, upgrade/0]).
13
14%% supervisor callbacks
15-export([init/1]).
16
17%% @spec start_link() -> ServerRet
18%% @doc API for starting the supervisor.
19start_link() ->
20    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
21
22%% @spec upgrade() -> ok
23%% @doc Add processes if necessary.
24upgrade() ->
25    {ok, {_, Specs}} = init([]),
26
27    Old = sets:from_list(
28            [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
29    New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
30    Kill = sets:subtract(Old, New),
31
32    sets:fold(fun (Id, ok) ->
33                      supervisor:terminate_child(?MODULE, Id),
34                      supervisor:delete_child(?MODULE, Id),
35                      ok
36              end, ok, Kill),
37
38    [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
39    ok.
40
41%% @spec init([]) -> SupervisorTree
42%% @doc supervisor callback.
43init([]) ->
44    Web = web_specs({{appid}}_web, {{port}}),
45    Processes = [Web],
46    Strategy = {one_for_one, 10, 10},
47    {ok,
48     {Strategy, lists:flatten(Processes)}}.
49
50web_specs(Mod, Port) ->
51    WebConfig = [{ip, {0,0,0,0}},
52                 {port, Port},
53                 {docroot, {{appid}}_deps:local_path(["priv", "www"])}],
54    {Mod,
55     {Mod, start, [WebConfig]},
56     permanent, 5000, worker, dynamic}.