|
|
| Author |
Message |
< Advanced Erlang/OTP ~ How to get to know that supervisor restarted a process? |
| qoocku |
Posted: Wed Jul 01, 2009 1:22 pm |
|
|
|
User
Joined: 31 Mar 2008
Posts: 10
Location: Poland
|
Hi list,
A problem:
A process wants to be informed that a supervisor has just restarted one of its child processes. Is it possible (AFAIK the supervisor code does not have any callbacks for this purposes)?
D. |
|
|
| Back to top |
|
| qoocku |
Posted: Thu Jul 02, 2009 10:13 am |
|
|
|
User
Joined: 31 Mar 2008
Posts: 10
Location: Poland
|
I have found a solution, but this hack afraids me a bit.
Having looked at the supervisor's source code I've noticed that just after a child process restart the supervisor calls
error_logger:info_report using progress report type with value:
Code:
[{supervisor, SupName}, {started, ChildInfo}]
which gave me an example of the solution:
* create event handler module x with functions:
Code:
init ([SupName]) ->
{ok, SupName}.
handle_event ({info_report, _, {_, progress, Report}}, State) -> ... ;
handle_event (_, State) ->
{ok, State}.
Now your init/1 function returns the event handler state value as your specific supervisor name/pid.
* handle_event(...) should contain a code to filter
reports which are related to your supervisor's children restars, for example:
Code:
case Report of
[SupInfo, StartInfo] ->
case SupInfo of
{supervisor, {State, module_of_your_supervisor}} ->
{started, ChildInfo} = StartInfo,
{pid, ChildPid} = hd(ChildInfo),
... here do something with this Pid ...
_ -> ok
end;
_ -> ok
end,
{ok, State}
Personaly, I'm using the extracted child pid to inform another process about the new child. ChildInfo contains several other interesting items which may be useful.
* The last thing is to register the event handler. The best place is in module_of_your_supervisor:start_link function when the brand new supervisor process pid is available:
Code:
error_logger:add_report_handler(x, [SupPid])
I'm looking for more elegant ways, but this worked so far. |
|
|
| Back to top |
|
| baryluk |
Posted: Tue Aug 18, 2009 11:48 am |
|
|
|
User
Joined: 05 Aug 2009
Posts: 48
|
This is very elegant solution. Only question is if this event will change in next releases of OTP.
Evetually you can use erlang:monitor(process, pid()), for knowing when process dies. (or use link(pid()) and process_flag(trap_exit, true), but i prefer monitor). This will only give you information about processes destroyed/killed/crashed/exited/ended, but not about new ones (created). |
|
|
| Back to top |
|
| satishbhawra37 |
Posted: Fri Sep 25, 2009 6:18 pm |
|
|
|
Joined: 25 Sep 2009
Posts: 2
|
|
| Back to top |
|
| absolute |
Posted: Mon Dec 14, 2009 7:08 pm |
|
|
|
Guest
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|