/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. %% @doc Supervisor for the webmachine application.
  17. -module(webmachine_sup).
  18. -behaviour(supervisor).
  19. %% External exports
  20. -export([start_link/0, upgrade/0, start_logger/1]).
  21. -export([start_perf_logger/1]).
  22. %% supervisor callbacks
  23. -export([init/1]).
  24. -include_lib("webmachine_logger.hrl").
  25. %% @spec start_link() -> ServerRet
  26. %% @doc API for starting the supervisor.
  27. start_link() ->
  28. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  29. start_logger(BaseDir) ->
  30. case application:get_env(webmachine, webmachine_logger_module) of
  31. {ok, LoggerModule} ->
  32. ChildSpec =
  33. {webmachine_logger,
  34. {LoggerModule, start_link, [BaseDir]},
  35. permanent, 5000, worker, dynamic},
  36. supervisor:start_child(?MODULE, ChildSpec);
  37. _ -> nop
  38. end.
  39. start_perf_logger(BaseDir) ->
  40. ChildSpec =
  41. {webmachine_perf_logger,
  42. {webmachine_perf_logger, start_link, [BaseDir]},
  43. permanent, 5000, worker, [webmachine_perf_logger]},
  44. supervisor:start_child(?MODULE, ChildSpec).
  45. %% @spec upgrade() -> ok
  46. %% @doc Add processes if necessary.
  47. upgrade() ->
  48. {ok, {_, Specs}} = init([]),
  49. Old = sets:from_list(
  50. [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
  51. New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
  52. Kill = sets:subtract(Old, New),
  53. sets:fold(fun (Id, ok) ->
  54. supervisor:terminate_child(?MODULE, Id),
  55. supervisor:delete_child(?MODULE, Id),
  56. ok
  57. end, ok, Kill),
  58. [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
  59. ok.
  60. %% @spec init([]) -> SupervisorTree
  61. %% @doc supervisor callback.
  62. init([]) ->
  63. init_wmtrace(),
  64. Processes = [],
  65. {ok, {{one_for_one, 9, 10}, Processes}}.
  66. init_wmtrace() ->
  67. Dir = "priv/wmtrace", %%TODO: move it to config file...
  68. ok = filelib:ensure_dir(filename:join(Dir, "test")),
  69. ets:new(?WMTRACE_CONF_TBL, [set, public, named_table]),
  70. ets:insert(?WMTRACE_CONF_TBL, {trace_dir, Dir}).