[erlang-questions] initializing process

Jayson Vantuyl kagato@REDACTED
Fri Sep 18 02:26:10 CEST 2009


What exactly are you trying to do?  It's really easy to create complex  
race conditions and strange startup ordering issues doing something  
like this.

If you can have the node signal you (much less work).  Just start out  
in an "uninitialized" state set a short timeout and just go idle.   
When you receive the "other_node_started" event, go into "running"  
mode.  I'd recommend using sync_send_event from the server you're  
waiting on.  That guarantees to close the loop in case they start up  
in a weird order.

Timeouts are handy and highly underutilized.  Other great uses include  
"forcibly garbage collecting during inactivity" and "delaying before  
hibernating".

If you must ping the node from the waiting side, you can simulate a  
delaying loop with a gen_server (and a gen_fsm) by using the timeout  
feature.  Just send the ping and set a timeout.  Have the timeout  
function send the ping again and set a timeout again.  Eventually,  
you'll get the response and can go about your merry way.

Both of these methods prevent you from sitting in init for an  
undefined amount of time.

All of this gets complicated in the face of restarts.  It may not  
really be workable.  It would help to know more about what you're doing.

I think the "correct" way (i.e. overkill in the style of Ericsson) is  
to do this at an application level.  You can set up a distributed  
Erlang application to start in "phases".  Depending on your  
deployment, that might be the ultimate answer, since it also provides  
hooks for takeover / failover, which may be necessary depending on  
your application.

On Sep 17, 2009, at 8:48 AM, paweł kamiński wrote:

> can somebody corrects me (and hopefully help with my problem).
>
> so the problem is like that
> 1)Im starting a server in erlang and among others I create one  
> process that
> needs to decide if another local computer is up before whole server's
> initialization process is done.
> 2)the process is simple gen_server
> (AND HERE IS A PROBLEM)
> 3)as long as I dont return from init/1 fun, my process is not seen  
> (I cant
> really use self() - is that correct??) by others so I CANT RECEIVE ANY
> MESSAGES. so I though I can spawn another processes while in init/1  
> that
> will loop and ping another side if it is ready. but I am afraid that  
> there
> might be a chance that the gen_server will not yet return from init  
> and
> spawned worker will try to send some notification, so or I will miss  
> it or
> there will be some error.
>
> is this really a problem or Im just missing something here that is so
> obvious.
> All I really need is to init state of the gen_server, then start  
> dynamically
> initializing the server so I cant hard code all conditions in init  
> fun and I
> need to communicate with other processes.
>
> hope it is more or less clear
>
> take care
>
> pozdrawiam
> Paweł Kamiński
>
> kamiseq@REDACTED
> pkaminski.prv@REDACTED
> ______________________



-- 
Jayson Vantuyl
kagato@REDACTED





More information about the erlang-questions mailing list