-
Notifications
You must be signed in to change notification settings - Fork 34
/
worker.lua
65 lines (61 loc) · 1.98 KB
/
worker.lua
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
-- Deregisters these workers from the list of known workers
function QlessWorker.deregister(...)
redis.call('zrem', 'ql:workers', unpack(arg))
end
-- Provide data about all the workers, or if a specific worker is provided,
-- then which jobs that worker is responsible for. If no worker is provided,
-- expect a response of the form:
--
-- [
-- # This is sorted by the recency of activity from that worker
-- {
-- 'name' : 'hostname1-pid1',
-- 'jobs' : 20,
-- 'stalled': 0
-- }, {
-- ...
-- }
-- ]
--
-- If a worker id is provided, then expect a response of the form:
--
-- {
-- 'jobs': [
-- jid1,
-- jid2,
-- ...
-- ], 'stalled': [
-- jid1,
-- ...
-- ]
-- }
--
function QlessWorker.counts(now, worker)
-- Clean up all the workers' job lists if they're too old. This is
-- determined by the `max-worker-age` configuration, defaulting to the
-- last day. Seems like a 'reasonable' default
local interval = tonumber(Qless.config.get('max-worker-age', 86400))
local workers = redis.call('zrangebyscore', 'ql:workers', 0, now - interval)
for index, worker in ipairs(workers) do
redis.call('del', 'ql:w:' .. worker .. ':jobs')
end
-- And now remove them from the list of known workers
redis.call('zremrangebyscore', 'ql:workers', 0, now - interval)
if worker then
return {
jobs = redis.call('zrevrangebyscore', 'ql:w:' .. worker .. ':jobs', now + 8640000, now),
stalled = redis.call('zrevrangebyscore', 'ql:w:' .. worker .. ':jobs', now, 0)
}
else
local response = {}
local workers = redis.call('zrevrange', 'ql:workers', 0, -1)
for index, worker in ipairs(workers) do
table.insert(response, {
name = worker,
jobs = redis.call('zcount', 'ql:w:' .. worker .. ':jobs', now, now + 8640000),
stalled = redis.call('zcount', 'ql:w:' .. worker .. ':jobs', 0, now)
})
end
return response
end
end