Просмотр исходного кода

Pass metadata information to the callback on process exit function.

Roberto Ostinelli 9 лет назад
Родитель
Сommit
13fd2bc54d
3 измененных файлов с 19 добавлено и 15 удалено
  1. 6 4
      README.md
  2. 3 3
      src/syn_backbone.erl
  3. 10 8
      test/syn_register_processes_SUITE.erl

+ 6 - 4
README.md

@@ -192,11 +192,12 @@ The `process_exit_callback` option allows you to specify the `module` and the `f
 
 The callback function is defined as:
 ```erlang
-CallbackFun = fun(Key, Pid, Reason) -> any().
+CallbackFun = fun(Key, Pid, Metadata, Reason) -> any().
 
 Types:
 	Key = any()
 	Pid = pid()
+	Metadata = any()
 	Reason = any()
 ```
 The `Key` and `Pid` are the ones of the process that exited with `Reason`.
@@ -205,11 +206,12 @@ For instance, if you want to print a log when a process exited:
 
 ```erlang
 -module(my_callback).
+-export([callback_on_process_exit/4]).
 
-callback_on_process_exit(Key, Pid, Reason) ->
+callback_on_process_exit(Key, Pid, Metadata, Reason) ->
 	error_logger:info_msg(
-		"Process with Key ~p and Pid ~p exited with reason ~p~n",
-		[Key, Pid, Reason]
+		"Process with Key ~p, Pid ~p and Metadata ~p exited with reason ~p~n",
+		[Key, Pid, Metadata, Reason]
 	)
 ```
 

+ 3 - 3
src/syn_backbone.erl

@@ -231,7 +231,7 @@ handle_info({'EXIT', Pid, Reason}, #state{
     %% do not lock backbone
     spawn(fun() ->
         %% check if pid is in table
-        case find_by_pid(Pid) of
+        case find_by_pid(Pid, with_meta) of
             undefined ->
                 case Reason of
                     normal -> ok;
@@ -239,7 +239,7 @@ handle_info({'EXIT', Pid, Reason}, #state{
                     _ ->
                         error_logger:warning_msg("Received an exit message from an unlinked process ~p with reason: ~p", [Pid, Reason])
                 end;
-            Key ->
+            {Key, Meta} ->
                 %% delete from table
                 remove_process_by_key(Key),
                 %% log
@@ -252,7 +252,7 @@ handle_info({'EXIT', Pid, Reason}, #state{
                 %% callback
                 case ProcessExitCallbackModule of
                     undefined -> ok;
-                    _ -> ProcessExitCallbackModule:ProcessExitCallbackFunction(Key, Pid, Reason)
+                    _ -> ProcessExitCallbackModule:ProcessExitCallbackFunction(Key, Pid, Meta, Reason)
                 end
         end
     end),

+ 10 - 8
test/syn_register_processes_SUITE.erl

@@ -55,7 +55,7 @@
 ]).
 
 %% internals
--export([process_exit_callback_dummy/3]).
+-export([process_exit_callback_dummy/4]).
 
 %% include
 -include_lib("common_test/include/ct.hrl").
@@ -373,12 +373,13 @@ single_node_when_mnesia_is_ram_callback_on_process_exit(_Config) ->
     %% start process
     Pid = syn_test_suite_helper:start_process(),
     %% register
-    ok = syn:register(<<"my proc">>, Pid),
+    Meta = {some, meta},
+    ok = syn:register(<<"my proc">>, Pid, Meta),
     %% kill process
     syn_test_suite_helper:kill_process(Pid),
     %% check callback were triggered
     receive
-        {exited, CurrentNode, <<"my proc">>, Pid, killed} -> ok
+        {exited, CurrentNode, <<"my proc">>, Pid, Meta, killed} -> ok
     after 2000 ->
         ok = process_exit_callback_was_not_called_from_local_node
     end.
@@ -545,19 +546,20 @@ two_nodes_when_mnesia_is_ram_callback_on_process_exit(Config) ->
     PidLocal = syn_test_suite_helper:start_process(),
     PidSlave = syn_test_suite_helper:start_process(SlaveNode),
     %% register
-    ok = syn:register(<<"local">>, PidLocal),
+    Meta = {some, meta},
+    ok = syn:register(<<"local">>, PidLocal, Meta),
     ok = syn:register(<<"slave">>, PidSlave),
     %% kill process
     syn_test_suite_helper:kill_process(PidLocal),
     syn_test_suite_helper:kill_process(PidSlave),
     %% check callback were triggered
     receive
-        {exited, CurrentNode, <<"local">>, PidLocal, killed} -> ok
+        {exited, CurrentNode, <<"local">>, PidLocal, Meta, killed} -> ok
     after 2000 ->
         ok = process_exit_callback_was_not_called_from_local_node
     end,
     receive
-        {exited, SlaveNode, <<"slave">>, PidSlave, killed} -> ok
+        {exited, SlaveNode, <<"slave">>, PidSlave, undefined, killed} -> ok
     after 2000 ->
         ok = process_exit_callback_was_not_called_from_slave_node
     end.
@@ -593,5 +595,5 @@ two_nodes_when_mnesia_is_disc_find_by_pid(Config) ->
 %% ===================================================================
 %% Internal
 %% ===================================================================
-process_exit_callback_dummy(Key, Pid, Reason) ->
-    global:send(syn_register_process_SUITE_result, {exited, node(), Key, Pid, Reason}).
+process_exit_callback_dummy(Key, Pid, Meta, Reason) ->
+    global:send(syn_register_process_SUITE_result, {exited, node(), Key, Pid, Meta, Reason}).