-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtimer.q
34 lines (26 loc) · 788 Bytes
/
timer.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/ timer jobs
timer.job:flip `name`func`time!"s*p"$\:()
timer.job,:(`;();0Wp)
\d .timer
/ merge record(y) into table(x) in reverse chronological order
merge:`time xdesc upsert
/ add new timer (f)unction with (n)ame and (t)i(m)e into (t)able
add:{[t;n;f;tm]
r:(n;f;gtime tm);
t:merge[t;$[0h>type tm;r;reverse flip r]];
t}
/ run timer job at (i)ndex from (t)able and current time tm
run:{[t;i;tm]
j:t i;
t:.[t;();_;i];
r:value (f:j `func),ltime tm;
if[not null r;t:merge[t;(j `name;f;tm+r)]];
t}
/ scan timer (t)able for runable jobs
loop:{[t;tm]
while[tm>=last tms:t `time;t:run[t;-1+count tms;tm]];
t}
/ helper function to generate repeating jobs
/ (d)elay, (e)nd (t)ime, (f)unction, tm:current time
until:{[d;et;f;tm]if[tm<et;@[value;f,tm;0N!];:d]}
.z.ts:loop`timer.job