<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">There's a project <a href="https://github.com/erlware/erlcron" style="font-family:Arial,Helvetica,sans-serif">https://github.com/erlware/erlcron</a> that implements jobs with a timer. It presently maintains persistence through a crontab config file, but it looks like you need something more advanced, so you can take a look at the code, the core of it is just in one module ecrn_agent, and perhaps add a persistence layer.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Alternatively, switching your existing implementation to store records in a mnesia disk table would likely be the easiest approach.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Best,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Serge</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 13, 2020 at 10:33 AM <a href="mailto:ludovic@demblans.com">ludovic@demblans.com</a> <<a href="mailto:ludovic@demblans.com">ludovic@demblans.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello,<br><br>I'm looking for advice about implementing a timer queue in erlang or elixir.<br><br>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).<br>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).<br><br>This is a subset of the `timer` module. But I need to persist the data to disk and reload on application start.<br><br>I will have a tiny amount of entries : around 1000.<br><br>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:<span style="color:rgb(51,51,51)">start_timer</span><span style="color:rgb(119,119,119)">(</span><span style="color:rgb(51,51,51)">max</span><span style="color:rgb(119,119,119)">(TimeStamp</span><span style="color:rgb(119,119,119)">,</span><span style="color:rgb(51,51,51)"> </span><span style="color:rgb(156,93,39)">1</span><span style="color:rgb(119,119,119)">),</span><span style="color:rgb(51,51,51)"> self</span><span style="color:rgb(119,119,119)">(),</span><span style="color:rgb(51,51,51)"> </span><span style="color:rgb(119,119,119)">{</span><span style="color:rgb(156,93,39)">run_deletion</span><span style="color:rgb(119,119,119)">,</span><span style="color:rgb(51,51,51)"> Key</span><span style="color:rgb(119,119,119)">})</span>`, 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.<br><br>I'm about to implement cancellation but the code becomes messy as I do both table management and data management in the same module.<br><br>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).<br><br>Thank you
</blockquote></div>