local state = redis.call('HGET', KEYS[1], 'state')
if state ~= 'SUCCEEDED' and state ~= 'PARTIAL_FAILED' and state ~= 'FAILED' and state ~= 'CANCELLED' then
return {'STATE_MISMATCH'}
end
local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
local expireAt = tonumber(redis.call('HGET', KEYS[1], 'expireAt') or '0')
if expireAt > now then return {'NOT_DUE', tostring(expireAt)} end
if not redis.call('HGET', KEYS[1], 'reconciledAt') then return {'NOT_RECONCILED'} end
local cursor = tonumber(redis.call('HGET', KEYS[1], 'cleanupCursor') or '0')
local count = tonumber(ARGV[2])
for index = 1, count do
local seq = cursor + index - 1
local member = ARGV[1] .. ':' .. seq
local shardKey = KEYS[6 + (index - 1) * 2 + 1]
local inboxKey = KEYS[6 + (index - 1) * 2 + 2]
local messageId = redis.call('HGET', shardKey, 'inboxMessageId')
if messageId and messageId ~= '' then
redis.pcall('XACK', inboxKey, ARGV[3], messageId)
redis.call('XDEL', inboxKey, messageId)
end
redis.call('ZREM', KEYS[4], member)
redis.call('ZREM', KEYS[5], member)
redis.call('ZREM', KEYS[6], member)
redis.call('DEL', shardKey)
if redis.call('XLEN', inboxKey) == 0 then
local pending = redis.pcall('XPENDING', inboxKey, ARGV[3])
if type(pending) == 'table' and tonumber(pending[1] or '0') == 0 then
redis.call('DEL', inboxKey)
end
end
end
local nextCursor = cursor + count
local total = tonumber(redis.call('HGET', KEYS[1], 'shardTotal') or '0')
if nextCursor < total then
redis.call('HSET', KEYS[1], 'cleanupCursor', nextCursor)
return {'OK', tostring(count), tostring(nextCursor)}
end
redis.call('DEL', KEYS[1])
redis.call('ZREM', KEYS[2], ARGV[1])
redis.call('ZREM', KEYS[3], ARGV[1])
return {'COMPLETED', tostring(count), tostring(nextCursor)}