Custom timer module

Ulf Wiger ulf@REDACTED
Mon Jan 13 17:14:00 CET 2020


FWIW, the KVDB project [1] implemented both priority queues and timers
(which quite flexible scheduling options), with persistence.

Example, from an Erlang Factory presentation 2013 [2]:

12> kvdb_cron:create_crontab(db, timers).
ok
13> kvdb_cron:add( db,timers,"{in 3 secs; 3 times}",[],kvdb_cron,testf,[]).
{ok,{q_key,<<>>,105729091563262,105729094536167}}
14>
CRON!! {{{2013,3,19},{21,38,14}},658320}
CRON!! {{{2013,3,19},{21,38,17}},655700}
CRON!! {{{2013,3,19},{21,38,20}},642523}

I haven't touched the code in a long time, but if there is sufficient
interest, I could be persuaded to lend support. :)

BR,
Ulf W

[1] https://github.com/Feuerlabs/kvdb
[2] http://www.erlang-factory.com/conference/SFBay2013/speakers/UlfWiger

Den mån 13 jan. 2020 kl 16:33 skrev ludovic@REDACTED <
ludovic@REDACTED>:

> Hello,
>
> I'm looking for advice about implementing a timer queue in erlang or
> elixir.
>
> Basically what I need is a queue where I can write a timestamp, a key, and
> a value. When the local time reaches the timestamp, I want to handle the
> key/value (I guess receiving a message with the key and fetch, or receiving
> the key/value).
> The timer must be cancelable as the value is very small but will lead to
> heavy computation. I want to be able to fetch a key from outside of the
> process at any time and run the computation (and then cancel the scheduled
> computation).
>
> This is a subset of the `timer` module. But I need to persist the data to
> disk and reload on application start.
>
> I will have a tiny amount of entries : around 1000.
>
> At the moment I have an ordered_set with `{Timestamp, UserKey} = Key` as
> keys, I lookup for the first key and send_after to self the `Key` with
> `erlang:start_timer(max(TimeStamp, 1), self(), {run_deletion, Key})`,
> which returns a ref that I keep in state so I can ignore timer messages
> with an older ref. Everything is done inside a gen_server where I load the
> table from disk after init. Also I have to write the table to disk after
> each update because it is very important that all events are handled.
>
> I'm about to implement cancellation but the code becomes messy as I do
> both table management and data management in the same module.
>
> I wonder if there are implementations of this pattern in the community ? I
> could use a priority queue, but it seems to me that the implementations use
> a fixed list of priorities, and not arbitrary priorities (like a timestamp).
>
> Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200113/1601f0d6/attachment.htm>


More information about the erlang-questions mailing list