88-module (ra_worker ).
99-behaviour (gen_server ).
1010
11+ -include (" ra.hrl" ).
1112-export ([start_link /1 ,
1213 queue_work /3 ]).
1314
1819 terminate /2 ,
1920 code_change /3 ]).
2021
21- -record (state , {}).
22+ -record (state , {log_id = " " :: unicode :chardata ()
23+ }).
2224
2325% %% ra worker responsible for doing background work for a ra server.
2426% %%
@@ -39,9 +41,11 @@ queue_work(Pid, FunOrMfa, ErrFun) when is_pid(Pid) ->
3941% %% gen_server callbacks
4042% %%===================================================================
4143
42- init (Config ) when is_map (Config ) ->
44+ init (#{ id : = Id } = Config ) when is_map (Config ) ->
4345 process_flag (trap_exit , true ),
44- {ok , # state {}}.
46+ LogId = maps :get (friendly_name , Config ,
47+ lists :flatten (io_lib :format (" ~w " , [Id ]))),
48+ {ok , # state {log_id = LogId }}.
4549
4650handle_call (_ , _From , State ) ->
4751 {reply , ok , State }.
@@ -52,17 +56,19 @@ handle_cast({work, FunOrMfa, ErrFun}, State) ->
5256 try erlang :apply (M , F , Args ) of
5357 _ ->
5458 ok
55- catch Type :Err :_Stack ->
56- % % TODO: log
59+ catch Type :Err :Stack ->
60+ ? WARN (" ~ts : worker encounted error ~0p of type ~s , Stack:~n~p " ,
61+ [State # state .log_id , Err , Type , Stack ]),
5762 ErrFun ({Type , Err }),
5863 ok
5964 end ;
6065 _ when is_function (FunOrMfa ) ->
6166 try FunOrMfa () of
6267 _ ->
6368 ok
64- catch Type :Err :_Stack ->
65- % % TODO: log
69+ catch Type :Err :Stack ->
70+ ? WARN (" ~ts : worker encounted error ~0p of type ~s , Stack:~n~p " ,
71+ [State # state .log_id , Err , Type , Stack ]),
6672 ErrFun ({Type , Err })
6773 end ,
6874 ok
0 commit comments