Permalink
Browse files

First pass at a method to adjust a job's priority without a 'put'

  • Loading branch information...
1 parent cab13b9 commit d81d426db102602880ff3cdc1e274b893f57c896 Dan Lecocq committed Apr 23, 2012
Showing with 32 additions and 0 deletions.
  1. +32 −0 priority.lua
View
@@ -0,0 +1,32 @@
+-- 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 == '' 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

0 comments on commit d81d426

Please sign in to comment.