emqttd_mod_sup.erl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqttd_mod_sup).
  17. -behaviour(supervisor).
  18. -include("emqttd.hrl").
  19. %% API
  20. -export([start_link/0, start_child/1, start_child/2, stop_child/1]).
  21. %% Supervisor callbacks
  22. -export([init/1]).
  23. %% Helper macro for declaring children of supervisor
  24. -define(CHILD(Mod, Type), {Mod, {Mod, start_link, []}, permanent, 5000, Type, [Mod]}).
  25. %%--------------------------------------------------------------------
  26. %% API
  27. %%--------------------------------------------------------------------
  28. start_link() ->
  29. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  30. start_child(ChildSpec) when is_tuple(ChildSpec) ->
  31. supervisor:start_child(?MODULE, ChildSpec).
  32. start_child(Mod, Type) when is_atom(Mod) andalso is_atom(Type) ->
  33. supervisor:start_child(?MODULE, ?CHILD(Mod, Type)).
  34. -spec(stop_child(any()) -> ok | {error, term()}).
  35. stop_child(ChildId) ->
  36. case supervisor:terminate_child(?MODULE, ChildId) of
  37. ok -> supervisor:delete_child(?MODULE, ChildId);
  38. Error -> Error
  39. end.
  40. %%--------------------------------------------------------------------
  41. %% Supervisor callbacks
  42. %%--------------------------------------------------------------------
  43. init([]) ->
  44. {ok, {{one_for_one, 10, 100}, []}}.