local state = redis.call('HGET', KEYS[1], 'state')
if not state then
return redis.error_reply('ERR Fanout cancellation root is absent')
end
if state == 'SUCCEEDED' or state == 'PARTIAL_FAILED' or state == 'FAILED' or state == 'CANCELLED' then
return {'ALREADY_COMPLETED', state}
end
if state ~= 'CREATING' and state ~= 'COMMITTED' and state ~= 'WAITING_CHILDREN' and state ~= 'CANCELLING' then
return redis.error_reply('ERR Fanout cancellation state is invalid')
end
local total = tonumber(redis.call('HGET', KEYS[1], 'shardTotal'))
local expected = tonumber(ARGV[4])
local count = tonumber(ARGV[5])
local retention = tonumber(ARGV[3])
local rescan = tonumber(ARGV[7])
local cursor = 0
if state == 'CANCELLING' then
cursor = tonumber(redis.call('HGET', KEYS[1], 'cancelCursor') or '0')
end
if #ARGV ~= 7 or redis.call('HGET', KEYS[1], 'fanoutId') ~= ARGV[1]
or not total or total < 0 or total > 9007199254740991 or total ~= math.floor(total)
or not cursor or cursor < 0 or cursor > total or cursor ~= math.floor(cursor)
or not expected or expected < 0 or expected > total or expected ~= math.floor(expected)
or not count or count < 0 or count > total - expected or count ~= math.floor(count)
or #KEYS ~= 7 + count * 2 or (count == 0 and expected < total)
or not retention or retention <= 0 or retention > 9007199254740991 or retention ~= math.floor(retention)
or not rescan or rescan <= 0 or rescan > 9007199254740991 or rescan ~= math.floor(rescan) then
return redis.error_reply('ERR Fanout cancellation arguments are invalid')
end
if cursor ~= expected then
return {'STALE', tostring(cursor)}
end
local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
if state ~= 'CANCELLING' then
redis.call('HSET', KEYS[1], 'state', 'CANCELLING', 'cancelReason', ARGV[2], 'cancelCursor', 0)
end
for index = 1, count do
local seq = cursor + index - 1
local member = ARGV[1] .. ':' .. seq
local shardKey = KEYS[7 + (index - 1) * 2 + 1]
local inboxKey = KEYS[7 + (index - 1) * 2 + 2]
local shardState = redis.call('HGET', shardKey, 'state')
if shardState == 'RUNNING' then
redis.call('HSET', shardKey, 'cancelRequestedAt', now)
elseif shardState ~= 'SUCCEEDED' and shardState ~= 'FAILED' and shardState ~= 'DEAD'
and shardState ~= 'SKIPPED' and shardState ~= 'CANCELLED' then
local messageId = redis.call('HGET', shardKey, 'inboxMessageId')
if messageId and messageId ~= '' then
redis.pcall('XACK', inboxKey, ARGV[6], messageId)
redis.call('XDEL', inboxKey, messageId)
end
redis.call('ZREM', KEYS[5], member)
redis.call('ZREM', KEYS[6], member)
redis.call('ZREM', KEYS[7], member)
redis.call('HSET', shardKey, 'state', 'CANCELLED', 'finishedAt', now,
'resultCode', 'CANCELLED', 'errorType', ARGV[2], 'owner', '', 'leaseUntil', '')
redis.call('HINCRBY', KEYS[1], 'cancelledCount', 1)
end
end
local nextCursor = cursor + count
if nextCursor < total then
redis.call('HSET', KEYS[1], 'cancelCursor', nextCursor, 'nextWatchAt', now)
redis.call('ZADD', KEYS[2], now, ARGV[1])
return {'OK', 'CANCELLING', tostring(nextCursor)}
end
local terminal = tonumber(redis.call('HGET', KEYS[1], 'successCount') or '0')
+ tonumber(redis.call('HGET', KEYS[1], 'failedCount') or '0')
+ tonumber(redis.call('HGET', KEYS[1], 'deadCount') or '0')
+ tonumber(redis.call('HGET', KEYS[1], 'skippedCount') or '0')
+ tonumber(redis.call('HGET', KEYS[1], 'cancelledCount') or '0')
if terminal < total then
local nextWatchAt = now + tonumber(ARGV[7])
redis.call('HSET', KEYS[1], 'cancelCursor', 0, 'nextWatchAt', nextWatchAt)
redis.call('ZADD', KEYS[2], nextWatchAt, ARGV[1])
return {'OK', 'CANCELLING', '0'}
end
local expireAt = now + tonumber(ARGV[3])
redis.call('HSET', KEYS[1], 'state', 'CANCELLED', 'finishedAt', now, 'expireAt', expireAt)
redis.call('ZADD', KEYS[4], expireAt, ARGV[1])
redis.call('XADD', KEYS[3], 'MAXLEN', '~', 100000, '*', 'event', 'FANOUT_COMPLETED',
'fanoutId', ARGV[1], 'rootRunId', redis.call('HGET', KEYS[1], 'rootRunId'),
'state', 'CANCELLED', 'cancelReason', redis.call('HGET', KEYS[1], 'cancelReason') or '')
return {'OK', 'CANCELLED', tostring(expireAt)}