[erlang-questions] Re: erl startup

Bernard Duggan bernie@REDACTED
Wed May 19 06:14:02 CEST 2010


On 19/05/10 13:46, Wes James wrote:
>
> I'm trying run my_mod which i'm running in conjunction with yaws so
> that the my_mod will every 5 minutes go and check a printer for home
> and grab the pages printed and stick it in a mnesia table.  I then use
> a .yaws page to look at the mnesia page count values.  This is why I'm
> trying to get the timer to work as I start yaws and keep going after
> yaws is started.
>    
Okay, so it seems like this issue here is the notion of "running a 
module".  I don't think it means what you think it means (actually I 
don't think it means anything at all, really).  What it sounds like you 
want to do is spawn a process that does some startup stuff and then 
basically sits idle to allow its ets table and timer to continue to work.
There's a couple of ways to approach this - the "OTP way(s)" would be to 
either wrap the whole thing in an application or gen_server behaviour.  
That gives you all sorts of nice extras (easy supervision, appearing in 
appmon etc) but does require a bit of scaffold code and has something of 
a learning curve before you figure out how to do it "right" (hell, I'm 
/still/ figuring the details of that out).
The much easier (though less flexible and powerful) way is to modify 
your code to something like this:

start() ->
	spawn_link(?MODULE, run, []).

run() ->
	Id_Table=ets:new(start_id_table, []),
	io:format("~w~n",  [Id_Table]),
	{ok, TRef}=timer:apply_interval(1000*60*5, prt, getc, []), % 1000
milliseconds * 60 * 5 = 5 minutes
	ets:insert(Id_Table, {id, TRef}),
	wait_for_exit().

wait_for_exit() ->
	receive
		some_exit_signal ->  ok;
		_ ->  wait_for_exit()
	end.


(Haven't compile or tested this - there's probably a syntax error or 
two).  Note the tail-recursive receive loop at the end which keeps the 
process alive and keeps its message queue empty but does nothing else.

Also, just as a tip: I recently discovered the timer:minutes/1 function 
and its friends - much easier to read and code than "1000*60*5" :)

Cheers,

Bernard



More information about the erlang-questions mailing list