syn_event_handler.erl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Portions of code from Ulf Wiger's unsplit server module:
  9. %% <https://github.com/uwiger/unsplit/blob/master/src/unsplit_server.erl>
  10. %%
  11. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  12. %% of this software and associated documentation files (the "Software"), to deal
  13. %% in the Software without restriction, including without limitation the rights
  14. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. %% copies of the Software, and to permit persons to whom the Software is
  16. %% furnished to do so, subject to the following conditions:
  17. %%
  18. %% The above copyright notice and this permission notice shall be included in
  19. %% all copies or substantial portions of the Software.
  20. %%
  21. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. %% THE SOFTWARE.
  28. %% ==========================================================================================================
  29. -module(syn_event_handler).
  30. -export([do_on_process_exit/5]).
  31. -export([do_on_group_process_exit/5]).
  32. -export([do_resolve_registry_conflict/4]).
  33. -callback on_process_exit(
  34. Name :: any(),
  35. Pid :: pid(),
  36. Meta :: any(),
  37. Reason :: any()
  38. ) -> any().
  39. -callback on_group_process_exit(
  40. GroupName :: any(),
  41. Pid :: pid(),
  42. Meta :: any(),
  43. Reason :: any()
  44. ) -> any().
  45. -callback resolve_registry_conflict(
  46. Name :: any(),
  47. {Pid1 :: pid(), Meta1 :: any()},
  48. {Pid2 :: pid(), Meta2 :: any()}
  49. ) -> PidToKeep :: pid() | undefined.
  50. -optional_callbacks([on_process_exit/4, on_group_process_exit/4, resolve_registry_conflict/3]).
  51. %% ===================================================================
  52. %% API
  53. %% ===================================================================
  54. -spec do_on_process_exit(
  55. Name :: any(),
  56. Pid :: pid(),
  57. Meta :: any(),
  58. Reason :: any(),
  59. CustomEventHandler :: module()
  60. ) -> any().
  61. do_on_process_exit(Name, Pid, Meta, Reason, CustomEventHandler) ->
  62. spawn(fun() ->
  63. case erlang:function_exported(CustomEventHandler, on_process_exit, 4) of
  64. true ->
  65. CustomEventHandler:on_process_exit(Name, Pid, Meta, Reason);
  66. _ ->
  67. ok
  68. end
  69. end).
  70. -spec do_on_group_process_exit(
  71. GroupName :: any(),
  72. Pid :: pid(),
  73. Meta :: any(),
  74. Reason :: any(),
  75. CustomEventHandler :: module()
  76. ) -> any().
  77. do_on_group_process_exit(GroupName, Pid, Meta, Reason, CustomEventHandler) ->
  78. spawn(fun() ->
  79. case erlang:function_exported(CustomEventHandler, on_group_process_exit, 4) of
  80. true ->
  81. CustomEventHandler:on_group_process_exit(GroupName, Pid, Meta, Reason);
  82. _ ->
  83. ok
  84. end
  85. end).
  86. -spec do_resolve_registry_conflict(
  87. Name :: any(),
  88. {Pid1 :: pid(), Meta1 :: any()},
  89. {Pid2 :: pid(), Meta2 :: any()},
  90. CustomEventHandler :: module()
  91. ) -> {PidToKeep :: pid() | undefined, KillOther :: boolean()}.
  92. do_resolve_registry_conflict(Name, {LocalPid, LocalMeta}, {RemotePid, RemoteMeta}, CustomEventHandler) ->
  93. case erlang:function_exported(CustomEventHandler, resolve_registry_conflict, 3) of
  94. true ->
  95. try CustomEventHandler:resolve_registry_conflict(Name, {LocalPid, LocalMeta}, {RemotePid, RemoteMeta}) of
  96. PidToKeep when is_pid(PidToKeep) ->
  97. {PidToKeep, false};
  98. _ ->
  99. {undefined, false}
  100. catch Class:Reason:Stacktrace ->
  101. error_logger:error_msg(
  102. "Syn(~p): Error in custom handler resolve_registry_conflict: ~p:~p:~p",
  103. [node(), Class, Reason, Stacktrace]
  104. ),
  105. {undefined, false}
  106. end;
  107. _ ->
  108. %% by default, keep local pid
  109. {LocalPid, true}
  110. end.