[erlang-questions] How to get a worker's pid() after restarted by supervisor?
fxmy wang
fxmywc@REDACTED
Fri Aug 30 03:43:09 CEST 2013
Thanks Dmitry for the advise :)
I worked out a way like bellow to automatically get informed new worker
process after restarted by supervisor.
If you can't get supervisor to voluntarily send restarted worker Pid, you
just let the worker himself to inform you :|
Though it seems kind of "ugly"
==================================
=====dispatcher.erl
==================================
-behaviour(gen_server).
%% Locally register( dispatcher, self() ).
...
handle_call( {startWorker, Args}, _From, State) ->
worker_sup:start_child( Arg),
receive
{workerStarted, Args, Pid} ->
%% store {Args, Pid} into ets table & monitor this
Pid.
{reply, {ok, Pid}, State}
after ?TIMEOUT ->
{reply, {cantStartWorker, Args}, State}
end.
%%This should happen only when a worker is restarted by supervisor
handle_info( {workerStared, Args, Pid}, State) ->
%% Update ets table & monitor Pid
{noreply, State};
%% This should happen when a worker exits normally
handle_info( {'DOWN', _Ref, process, Pid, normal}, State) ->
%% Update ets table
{noreply, State}.
========================================
=====worker.erl
========================================
-behaviour(gen_server).
init(Args) ->
%% Do things necessary
dispatcher ! {workerStarted, Args, self() },
{ok, State}.
So far seems good ,cheers XD
B.R.
2013/8/29 Dmitry Kolesnikov <dmkolesnikov@REDACTED>
> Hello,
>
> You have to maintain mapping of uri to pid in external table (e.g ETS)
> However, there is number of open source project that addresses this issue.
> For example gproc by Ulf.
>
> However, I had exactly the same task / problem like your and made a
> library to manage those processes. You can find it here:
> http://github.com/fogfish/pts
>
>
> Best Regards,
> Dmitry >-|-|-*>
>
>
> On 29.8.2013, at 10.03, fxmy wang <fxmywc@REDACTED> wrote:
>
> >
> > Hi dear fellow erlangers,
> >
> > New to Erlang, I'm building a small comment backend for practice. By my
> design there are many worker processes that handle comments posted from
> different URLs, and these workers are all under simple_one_for_one
> supervision.
> >
> > I also decide to start a dispatcher process to map certain URL comment
> to corresponding worker by worker_sup:start_child(URL) and then store
> returned Pid.
> >
> > So here comes the problem, I expect that worker may be restarted by
> supervisor. Is there a way to get the worker's Pid after restarted by
> supervisor, so that I can keep track of which URL comment should go for
> which worker process?
> >
> > Hope express myself clearly cause not a native speaker :)
> >
> > B.R.
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130830/b55d0fe7/attachment.htm>
More information about the erlang-questions
mailing list