local worker_key_prefix = KEYS[4]
local workers
if ARGV[2] then
local score = redis.call("zscore", KEYS[1], ARGV[2])
if score and tonumber(score) <= tonumber(ARGV[1]) then
workers = {ARGV[2]}
else
workers = {}
end
else
workers = redis.call("zrangebyscore", KEYS[1], 0, ARGV[1])
end
redis.replicate_commands()
local processed = 0
for _, worker in ipairs(workers) do
local inflight_set_key = worker_key_prefix .. ":" .. worker
local jobs = redis.call("smembers", inflight_set_key)
local count = table.getn(jobs)
if count > 0 then
redis.call("rpush", KEYS[2], unpack(jobs))
redis.call("del", inflight_set_key)
processed = processed + count
end
redis.call("zrem", KEYS[1], worker)
end
if processed > 0 then
redis.call("del", KEYS[3])
redis.call("lpush", KEYS[3], 1)
end
return processed