[erlang-questions] Issue while starting same worker(gen_server) multiple times in Supervisor

Rad Gruchalski <>
Thu Mar 26 19:13:24 CET 2015


The problem is in the worker:

-module(uclient_worker).
...
start_link(Pars) ->
    %%file:write_file("/tmp/rabbit.txt", Pars, [append]),
    gen_server:start_link({global, ?MODULE}, ?MODULE, Pars, []).


?MODULE is expanded to uclient_worker at compile time and registered globally using {global, ?MODULE}, your child is starting twice under {global, uclient_worker}, hence the error. What’s after global, the name of the instance, has to be unique when starting multiple times. Same applies to local.

What you most likely want to do, is the following:

-define(
   CHILD(Name, Args),
   {Name, {
       uclient_worker, start_link, [{Name, <<"callmgr">> , 3}]
       }, permanent, 5000, worker, []
   }
).

…
% use an atom here, not strings...
Children = [?CHILD(uc1, Address), ?CHILD(uc2, Address)],

And then, in the worker:

start_link({ Name, Bin, IntArg }) ->
    gen_server:start_link({global, Name}, ?MODULE, { Bin, Args }, []).


Hope it makes sense!  










Kind regards,

Radek Gruchalski

 (mailto:)
 (mailto:)
de.linkedin.com/in/radgruchalski/ (http://de.linkedin.com/in/radgruchalski/)

Confidentiality:
This communication is intended for the above-named person and may be confidential and/or legally privileged.
If it has come to you in error you must take no action based on it, nor must you copy or show it to anyone; please delete/destroy and inform the sender immediately.



On Thursday, 26 March 2015 at 17:59, harsha sri wrote:

> Hi Rad,
>  
> First of all Thank you so much for immediate reply.
>  
> uclient worker start method:
>  
>  
>  
> -module(uclient_worker).
> -behaviour(gen_server).
>  
> -export([start_link/1]).
>  
> -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
>          terminate/2, code_change/3]).
>  
> -export([fire/0]).
>  
> -export([add_subscriber/4, spawn_subscriber/3]).
>  
> -include_lib("amqp_client/include/amqp_client.hrl").
>  
> -record(state, {channel,java_port :: port(),java_node :: atom(),queuen,nofs}).
>  
> -define(RKFormat,
>         "~4.10.0B.~2.10.0B.~2.10.0B.~1.10.0B.~2.10.0B.~2.10.0B.~2.10.0B").
>  
>  
> start_link(Pars) ->
>     %%file:write_file("/tmp/rabbit.txt", Pars, [append]),
>     gen_server:start_link({global, ?MODULE}, ?MODULE, Pars, []).
>  
> %---------------------------
> % Gen Server Implementation
> % --------------------------
>  
> init(Pars) ->
>  
>  
> Regarding to CHILD in supervisor:
>  
> actually my initial supervisor is :
> %%%%%%%%
> -module(uclient_sup).
>  
> -behaviour(supervisor).
>  
> -export([start_link/0, init/1]).
>  
> start_link() ->
>     supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg = []).
>  
> init([]) ->
>     {ok, {{one_for_one, 1, 10},
>           [{uclient_worker,
>             {uclient_worker, start_link, [{<<"callmgr">> , 3}]},
>             permanent,
>             10,
>             worker,
>             [uclient_worker]}
>           ]}}.
> %%%%%%%%%%%%%
>  
>  
> But i changed above code for invoking multiple worker under supervisor based on conditions.
> In that process i just changed code using CHILD.
>  
> Regards,
> Harsha
>  
>  
>  
> On Thu, Mar 26, 2015 at 9:41 PM, Rad Gruchalski < (mailto:)> wrote:
> > Can you show start_link of the uclient_worker?
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> >  
> > Kind regards,

> > Radek Gruchalski
> > 
 (mailto:)
 (mailto:)
> > de.linkedin.com/in/radgruchalski/ (http://de.linkedin.com/in/radgruchalski/)
> >  
> > Confidentiality:
> > This communication is intended for the above-named person and may be confidential and/or legally privileged.
> > If it has come to you in error you must take no action based on it, nor must you copy or show it to anyone; please delete/destroy and inform the sender immediately.
> >  
> >  
> >  
> > On Thursday, 26 March 2015 at 15:49, harsha sri wrote:
> >  
> >  
> >  
> > > Hi,
> > >  
> > > I am trying to start same worker multiple times in Supervisor but its giving me error.
> > > Here is my Supervisor:
> > >  
> > >  
> > > -module(uclient_sup).
> > >  
> > > -behaviour(supervisor).
> > >  
> > > -export([start_link/0, init/1]).
> > >  
> > > -define(
> > >    CHILD(Name, Args),
> > >    {Name, {
> > >        uclient_worker, start_link, [{<<"callmgr">> , 3}]
> > >        }, permanent, 5000, worker, []
> > >    }
> > > ).
> > >  
> > > start_link() ->
> > >     supervisor:start_link({global, ?MODULE}, ?MODULE, _Arg = []).
> > >  
> > > init([]) ->
> > >    Children = [?CHILD("uc1", Address), ?CHILD("uc2", Address)],
> > >    {ok, { {one_for_one, 6, 10}, Children }}.
> > >  
> > > I am getting below error:
> > >  
> > > Error: {could_not_start,uclient,
> > >            {{shutdown,
> > >                 {failed_to_start_child,"uc2",{already_started,<5169.429.0>}}},
> > >             {uclient,start,[normal,[]]}}}
> > >  
> > >  
> > > Please help me in solving this issue. I am new to erlang.
> > >  
> > > Thanks & Regards,
> > > SriHarsha.
> > > _______________________________________________
> > > erlang-questions mailing list
> > >  (mailto:)
> > > http://erlang.org/mailman/listinfo/erlang-questions
> > >  
> > >  
> > >  
> >  
> >  
>  

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150326/2ad9febf/attachment-0001.html>


More information about the erlang-questions mailing list