I need periodically cleanup my table. For this purpose I written small module:
-module(mod_timer).
-export([start/2,
stop/1, loop/1]).
-define(PROCNAME, mod_timer_loop).
start(_Host, _Opts) ->
case whereis(?PROCNAME) of
undefined ->
register(?PROCNAME, spawn(?MODULE, loop, [])),
?PROCNAME ! check;
_ ->
ok
end.
loop() ->
receive
check ->
timer:sleep(30000),
... here check something, e.g. clear db ...
?PROCNAME ! check,
loop();
stop ->
finished
end.
stop(_Host) ->
?PROCNAME ! stop.
It work, but(!) I don’t like loop() call in loop(). (I mean recursive call).
But in many modules I saw such mechansm.
Tell me how bad is it really?
Or how it can be done properly?
Thanks.
Try mod_cron
Another solution is to use timer:apply_interval. I wrote a small module that provides the feature you want using that solution: .