Skip to content
Browse files

Tagging a non-existent job should be a no-op.

  • Loading branch information...
1 parent c26fcf6 commit 09bc1dcf6255e720ee0afba4a3b45026f6f0525e Dan Lecocq committed Apr 23, 2012
Showing with 44 additions and 44 deletions.
  1. +44 −44 tag.lua
View
88 tag.lua
@@ -28,62 +28,62 @@ if command == 'add' then
local now = assert(tonumber(ARGV[3]), 'Tag(): Arg "now" is not a number')
local tags = redis.call('hget', 'ql:j:' .. jid, 'tags')
-- If the job has been canceled / deleted, then return false
- if tags == nil then
- return false
- end
-
- -- Decode the json blob, convert to dictionary
- tags = cjson.decode(tags)
- local _tags = {}
- for i,v in ipairs(tags) do
- _tags[v] = true
- end
+ if tags then
+ -- Decode the json blob, convert to dictionary
+ tags = cjson.decode(tags)
+ local _tags = {}
+ for i,v in ipairs(tags) do
+ _tags[v] = true
+ end
- -- Otherwise, add the job to the sorted set with that tags
- for i=4,#ARGV do
- local tag = ARGV[i]
- if _tags[tag] == nil then
- table.insert(tags, tag)
+ -- Otherwise, add the job to the sorted set with that tags
+ for i=4,#ARGV do
+ local tag = ARGV[i]
+ if _tags[tag] == nil then
+ table.insert(tags, tag)
+ end
+ redis.call('zadd', 'ql:t:' .. tag, now, jid)
end
- redis.call('zadd', 'ql:t:' .. tag, now, jid)
- end
- tags = cjson.encode(tags)
- redis.call('hset', 'ql:j:' .. jid, 'tags', tags)
- return tags
+ tags = cjson.encode(tags)
+ redis.call('hset', 'ql:j:' .. jid, 'tags', tags)
+ return tags
+ else
+ return false
+ end
elseif command == 'remove' then
local jid = assert(ARGV[2] , 'Tag(): Arg "jid" missing')
local now = assert(tonumber(ARGV[3]), 'Tag(): Arg "now" is not a number')
local tags = redis.call('hget', 'ql:j:' .. jid, 'tags')
-- If the job has been canceled / deleted, then return false
- if tags == nil then
- return false
- end
-
- -- Decode the json blob, convert to dictionary
- tags = cjson.decode(tags)
- local _tags = {}
- for i,v in ipairs(tags) do
- _tags[v] = true
- end
+ if tags then
+ -- Decode the json blob, convert to dictionary
+ tags = cjson.decode(tags)
+ local _tags = {}
+ for i,v in ipairs(tags) do
+ _tags[v] = true
+ end
- -- Otherwise, add the job to the sorted set with that tags
- for i=4,#ARGV do
- local tag = ARGV[i]
- _tags[tag] = nil
- redis.call('zrem', 'ql:t:' .. tag, jid)
- end
+ -- Otherwise, add the job to the sorted set with that tags
+ for i=4,#ARGV do
+ local tag = ARGV[i]
+ _tags[tag] = nil
+ redis.call('zrem', 'ql:t:' .. tag, jid)
+ end
- local results = {}
- for i,tag in ipairs(tags) do
- if _tags[tag] then
- table.insert(results, tag)
+ local results = {}
+ for i,tag in ipairs(tags) do
+ if _tags[tag] then
+ table.insert(results, tag)
+ end
end
- end
- tags = cjson.encode(results)
- redis.call('hset', 'ql:j:' .. jid, 'tags', tags)
- return tags
+ tags = cjson.encode(results)
+ redis.call('hset', 'ql:j:' .. jid, 'tags', tags)
+ return tags
+ else
+ return false
+ end
elseif command == 'get' then
local tag = assert(ARGV[2] , 'Tag(): Arg "tag" missing')
local offset = assert(tonumber(ARGV[3] or 0) , 'Tag(): Arg "offset" not a number: ' .. tostring(ARGV[3]))

0 comments on commit 09bc1dc

Please sign in to comment.
Something went wrong with that request. Please try again.