forked from seomoz/qless-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkers.lua
More file actions
69 lines (64 loc) · 2.02 KB
/
workers.lua
File metadata and controls
69 lines (64 loc) · 2.02 KB
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
-- Workers(0, now, [worker])
-- -------------------------
-- 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,
-- ...
-- ]
-- }
--
if #KEYS > 0 then
error('Workers(): No key arguments expected')
end
local now = assert(tonumber(ARGV[1]), 'Workers(): Arg "now" missing or not a number: ' .. (ARGV[1] or 'nil'))
-- 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(
redis.call('hget', 'ql:config', 'max-worker-age')) or 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 #ARGV == 1 then
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 cjson.encode(response)
else
local worker = assert(ARGV[2], 'Workers(): Arg "worker" missing.')
local response = {
jobs = redis.call('zrevrangebyscore', 'ql:w:' .. worker .. ':jobs', now + 8640000, now),
stalled = redis.call('zrevrangebyscore', 'ql:w:' .. worker .. ':jobs', now, 0)
}
return cjson.encode(response)
end