syn_scope_sup.erl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2022 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. %% @private
  27. -module(syn_scope_sup).
  28. -behaviour(supervisor).
  29. %% API
  30. -export([start_link/1]).
  31. %% Supervisor callbacks
  32. -export([init/1]).
  33. %% ===================================================================
  34. %% API
  35. %% ===================================================================
  36. -spec start_link(Scope :: atom()) -> {ok, pid()} | {already_started, pid()} | shutdown.
  37. start_link(Scope) ->
  38. supervisor:start_link(?MODULE, [Scope]).
  39. %% ===================================================================
  40. %% Callbacks
  41. %% ===================================================================
  42. -spec init([Scope :: atom()]) ->
  43. {ok, {{supervisor:strategy(), non_neg_integer(), pos_integer()}, [supervisor:child_spec()]}}.
  44. init([Scope]) ->
  45. %% create ETS tables
  46. ok = syn_backbone:create_tables_for_scope(Scope),
  47. %% set children
  48. Children = [
  49. scope_child_spec(syn_registry, Scope),
  50. scope_child_spec(syn_pg, Scope)
  51. ],
  52. {ok, {{one_for_one, 10, 10}, Children}}.
  53. %% ===================================================================
  54. %% Internals
  55. %% ===================================================================
  56. -spec scope_child_spec(module(), Scope :: atom()) -> supervisor:child_spec().
  57. scope_child_spec(Module, Scope) ->
  58. #{
  59. id => {Module, Scope},
  60. start => {Module, start_link, [Scope]},
  61. type => worker,
  62. shutdown => 10000,
  63. restart => permanent,
  64. modules => [Module]
  65. }.