PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/webmachine/src/webmachine_sup.erl

http://github.com/zotonic/zotonic
Erlang | 85 lines | 46 code | 15 blank | 24 comment | 3 complexity | 951bbe00d37c249d56883ab5965d4c99 MD5 | raw file
 1%% @author Justin Sheehy <justin@basho.com>
 2%% @author Andy Gross <andy@basho.com>
 3%% @copyright 2007-2008 Basho Technologies
 4%%
 5%%    Licensed under the Apache License, Version 2.0 (the "License");
 6%%    you may not use this file except in compliance with the License.
 7%%    You may obtain a copy of the License at
 8%%
 9%%        http://www.apache.org/licenses/LICENSE-2.0
10%%
11%%    Unless required by applicable law or agreed to in writing, software
12%%    distributed under the License is distributed on an "AS IS" BASIS,
13%%    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14%%    See the License for the specific language governing permissions and
15%%    limitations under the License.
16
17%% @doc Supervisor for the webmachine application.
18
19-module(webmachine_sup).
20
21-behaviour(supervisor).
22
23%% External exports
24-export([start_link/0, upgrade/0, start_logger/1]).
25-export([start_perf_logger/1]).
26
27%% supervisor callbacks
28-export([init/1]).
29
30-include_lib("webmachine_logger.hrl").
31
32%% @spec start_link() -> ServerRet
33%% @doc API for starting the supervisor.
34start_link() ->
35    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
36
37start_logger(BaseDir) ->
38    case application:get_env(webmachine, webmachine_logger_module) of
39        {ok, LoggerModule} ->
40            ChildSpec = 
41                {webmachine_logger,
42                 {LoggerModule, start_link, [BaseDir]},
43                 permanent, 5000, worker, dynamic},
44            supervisor:start_child(?MODULE, ChildSpec);
45        _ -> nop
46    end.
47
48start_perf_logger(BaseDir) ->
49    ChildSpec = 
50	{webmachine_perf_logger,
51	 {webmachine_perf_logger, start_link, [BaseDir]},
52	 permanent, 5000, worker, [webmachine_perf_logger]},
53    supervisor:start_child(?MODULE, ChildSpec).
54
55%% @spec upgrade() -> ok
56%% @doc Add processes if necessary.
57upgrade() ->
58    {ok, {_, Specs}} = init([]),
59
60    Old = sets:from_list(
61	    [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
62    New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
63    Kill = sets:subtract(Old, New),
64
65    sets:fold(fun (Id, ok) ->
66		      supervisor:terminate_child(?MODULE, Id),
67		      supervisor:delete_child(?MODULE, Id),
68		      ok
69	      end, ok, Kill),
70
71    [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
72    ok.
73
74%% @spec init([]) -> SupervisorTree
75%% @doc supervisor callback.
76init([]) ->    
77    init_wmtrace(),
78    Processes = [],
79    {ok, {{one_for_one, 9, 10}, Processes}}.
80    
81init_wmtrace() ->
82    Dir = "priv/wmtrace", %%TODO: move it to config file...
83    ok = filelib:ensure_dir(filename:join(Dir, "test")),		
84    ets:new(?WMTRACE_CONF_TBL, [set, public, named_table]),
85    ets:insert(?WMTRACE_CONF_TBL, {trace_dir, Dir}).