local worker_inflight_set = KEYS[3]
local worker_name = ARGV[3]
local registered = redis.call("zscore", KEYS[1], worker_name)
if not registered then
error("Cant fetch next: worker not registered")
end
local scheduled_ids = redis.call(
"zrangebyscore",
KEYS[7],
0,
ARGV[1]
)
if #scheduled_ids > 0 then
redis.call("rpush", KEYS[2], unpack(scheduled_ids))
redis.call("zrem", KEYS[7], unpack(scheduled_ids))
redis.call("del", KEYS[5])
redis.call("lpush", KEYS[5], 1)
end
local task_ids = redis.call(
"lrange",
KEYS[2],
0,
tonumber(ARGV[2]) - 1
)
local count = #task_ids
local results = {}
local meta = {}
if count > 0 then
redis.call("sadd", worker_inflight_set, unpack(task_ids))
redis.call("ltrim", KEYS[2], count, -1)
results = redis.call(
"hmget",
KEYS[4],
unpack(task_ids)
)
for _, task_id in ipairs(task_ids) do
local meta_key = KEYS[6] .. ":" .. task_id
local fields = redis.call("hgetall", meta_key)
table.insert(fields, 1, task_id)
table.insert(meta, fields)
end
end
if count < tonumber(ARGV[2]) then
redis.call("del", KEYS[5])
end
return {results, meta}