forked from seomoz/qless-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.lua
More file actions
34 lines (30 loc) · 1.19 KB
/
priority.lua
File metadata and controls
34 lines (30 loc) · 1.19 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
-- priority(0, jid, priority)
-- --------------------------
-- Accepts a jid, and a new priority for the job. If the job
-- doesn't exist, then return false. Otherwise, return the
-- updated priority. If the job is waiting, then the change
-- will be reflected in the order in which it's popped
if #KEYS ~= 0 then
error('Priority(): Got ' .. #KEYS .. ', expected 0')
end
local jid = assert(ARGV[1] , 'Priority(): Arg "jid" missing')
local priority = assert(tonumber(ARGV[2]), 'Priority(): Arg "priority" missing or not a number: ' .. tostring(ARGV[2]))
-- Get the queue the job is currently in, if any
local queue = redis.call('hget', 'ql:j:' .. jid, 'queue')
if queue == nil then
return false
elseif queue == false then
return false
elseif queue == '' then
-- Just adjust the priority
redis.call('hset', 'ql:j:' .. jid, 'priority', priority)
return priority
else
-- Adjust the priority and see if it's a candidate for updating
-- its priority in the queue it's currently in
if redis.call('zscore', 'ql:q:' .. queue .. '-work', jid) then
redis.call('zadd', 'ql:q:' .. queue .. '-work', priority, jid)
end
redis.call('hset', 'ql:j:' .. jid, 'priority', priority)
return priority
end