syn_backbone.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 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. -module(syn_backbone).
  27. %% API
  28. -export([init/0]).
  29. -export([get_event_handler_module/0]).
  30. %% macros
  31. -define(DEFAULT_EVENT_HANDLER_MODULE, syn_event_handler).
  32. %% includes
  33. -include("syn.hrl").
  34. %% ===================================================================
  35. %% API
  36. %% ===================================================================
  37. -spec init() -> ok | {error, Reason :: any()}.
  38. init() ->
  39. drop_tables(),
  40. case create_registry_table() of
  41. {atomic, ok} ->
  42. case create_groups_table() of
  43. {atomic, ok} -> ok;
  44. {aborted, Reason} -> {error, {could_not_create_syn_groups_table, Reason}}
  45. end;
  46. {aborted, Reason} ->
  47. {error, {could_not_create_syn_registry_table, Reason}}
  48. end.
  49. -spec get_event_handler_module() -> module().
  50. get_event_handler_module() ->
  51. %% get handler
  52. CustomEventHandler = application:get_env(syn, event_handler, ?DEFAULT_EVENT_HANDLER_MODULE),
  53. %% ensure that is it loaded (not using code:ensure_loaded/1 to support embedded mode)
  54. catch CustomEventHandler:module_info(exports),
  55. %% return
  56. CustomEventHandler.
  57. %% ===================================================================
  58. %% Internal
  59. %% ===================================================================
  60. -spec drop_tables() -> ok.
  61. drop_tables() ->
  62. mnesia:delete_table(syn_registry_table),
  63. mnesia:delete_table(syn_groups_table).
  64. -spec create_registry_table() -> {atomic, ok} | {aborted, Reason :: any()}.
  65. create_registry_table() ->
  66. mnesia:create_table(syn_registry_table, [
  67. {type, set},
  68. {attributes, record_info(fields, syn_registry_table)},
  69. {index, [#syn_registry_table.pid, #syn_groups_table.node]},
  70. {storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
  71. ]).
  72. -spec create_groups_table() -> {atomic, ok} | {aborted, Reason :: any()}.
  73. create_groups_table() ->
  74. mnesia:create_table(syn_groups_table, [
  75. {type, bag},
  76. {attributes, record_info(fields, syn_groups_table)},
  77. {index, [#syn_groups_table.pid, #syn_groups_table.node]},
  78. {storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
  79. ]).