-
Notifications
You must be signed in to change notification settings - Fork 1
/
oJobQueue.yaml
82 lines (74 loc) · 2.33 KB
/
oJobQueue.yaml
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
jobs:
# --------------------
- name: oJob Queue Add
exec: |
global.oJobQueue.add(args);
# -----------------------
- name: oJob Queue Delete
exec: |
global.oJobQueue.del(args);
# ----------------------
- name: oJob Queue Rules
exec: |
args.rules = _$(args.rules, "rules").isArray().default([]);
args.data = _$(args.data, "data").isMap().default({});
args.logFn = _$(args.logFn, "logFn").isFunction().default((r, ev) => {
log("Queue | Trigger job '" + r.job + "'.");
});
args.errFn = _$(args.errFn, "errFn").isFunction().default((r, ev, e) => {
logErr("Queue | Error in rule for job '" + r.job + "', evaluated as '" + ev + "': " + String(e));
});
var cexpr = "";
args.rules.map(r => {
try {
cexpr = templify(r.rule, args);
if (eval(cexpr)) {
args.logFn(r, cexpr);
global.oJobQueue.add({
job : r.job,
data: args
})
}
} catch(e) {
args.errFn(r, cexpr, e);
}
});
# ------------------------
- name: oJob Queue Manager
exec: |
args.queueTimeout = _$(args.queueTimeout, "queueTimeout").isNumber().default(60000);
args.queuePeriod = _$(args.queuePeriod, "queuePeriod").isNumber().default(5000);
args.queueDelete = _$(args.queueDelete, "queueDelete").isBoolean().default(true);
ow.loadServer();
global.__oJobQueue = new ow.server.queue();
plugin("Threads");
var t = new Threads();
t.addSingleThread(function() {
while(1) {
while (global.__oJobQueue.size() > 0) {
try {
var msg = global.__oJobQueue.receive(args.queueTimeout);
if (isDef(msg)) {
oJobRunJob(msg.obj.job, msg, genUUID());
if (args.queueDelete) global.__oJobQueue.delete(msg.idx);
}
} catch(e) {
}
}
sleep(args.queuePeriod, true);
}
});
global.oJobQueue = {
add: function(arg) {
if (isDef(global.__oJobQueue) && isDef(arg.job) && isDef(arg.data)) {
global.__oJobQueue.send({
job : arg.job,
data: arg.data
}, void 0, arg.timeout);
}
},
del: function(arg) {
if (isDef(global.__oJobQueue) && isDef(arg.idx)) global.__oJobQueue.delete(arg.idx);
}
}
t.startNoWait();