PageRenderTime 56ms CodeModel.GetById 50ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/priv/skel/src/skel_sup.erl

http://github.com/basho/mochiweb
Erlang | 62 lines | 34 code | 14 blank | 14 comment | 3 complexity | 25caa315c3635cdc015cb0dbe3be986a MD5 | raw file
 1%% @author author <author@example.com>
 2%% @copyright YYYY author.
 3
 4%% @doc Supervisor for the skel application.
 5
 6-module(skel_sup).
 7-author('author <author@example.com>').
 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    Ip = case os:getenv("MOCHIWEB_IP") of false -> "0.0.0.0"; Any -> Any end,
45    WebConfig = [
46         {ip, Ip},
47                 {port, 8000},
48                 {docroot, skel_deps:local_path(["priv", "www"])}],
49    Web = {skel_web,
50           {skel_web, start, [WebConfig]},
51           permanent, 5000, worker, dynamic},
52
53    Processes = [Web],
54    {ok, {{one_for_one, 10, 10}, Processes}}.
55
56
57%%
58%% Tests
59%%
60-include_lib("eunit/include/eunit.hrl").
61-ifdef(TEST).
62-endif.