local shardState = redis.call('HGET', KEYS[2], 'state')
if shardState == 'SUCCEEDED' or shardState == 'FAILED' or shardState == 'DEAD'
or shardState == 'SKIPPED' or shardState == 'CANCELLED' then return {'ALREADY_COMPLETED'} end
local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
redis.call('HSET', KEYS[2], 'state', ARGV[3], 'finishedAt', now,
'resultCode', ARGV[4], 'resultSummary', ARGV[5], 'owner', '', 'leaseUntil', '')
redis.call('ZREM', KEYS[3], ARGV[1] .. ':' .. ARGV[2])
redis.call('ZREM', KEYS[4], ARGV[1] .. ':' .. ARGV[2])
redis.call('ZREM', KEYS[5], ARGV[1] .. ':' .. ARGV[2])
local counter = ARGV[3] == 'SUCCEEDED' and 'successCount'
or (ARGV[3] == 'FAILED' and 'failedCount'
or (ARGV[3] == 'DEAD' and 'deadCount'
or (ARGV[3] == 'SKIPPED' and 'skippedCount' or 'cancelledCount')))
redis.call('HINCRBY', KEYS[1], counter, 1)
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')
local total = tonumber(redis.call('HGET', KEYS[1], 'shardTotal') or '0')
if terminal == total then
local failed = tonumber(redis.call('HGET', KEYS[1], 'failedCount') or '0')
+ tonumber(redis.call('HGET', KEYS[1], 'deadCount') or '0')
local skipped = tonumber(redis.call('HGET', KEYS[1], 'skippedCount') or '0')
local cancelled = tonumber(redis.call('HGET', KEYS[1], 'cancelledCount') or '0')
local rootState = failed > 0 and 'FAILED' or ((skipped > 0) and 'PARTIAL_FAILED'
or ((cancelled == total) and 'CANCELLED' or 'SUCCEEDED'))
local expireAt = now + tonumber(ARGV[6])
redis.call('HSET', KEYS[1], 'state', rootState, 'finishedAt', now, 'expireAt', expireAt)
redis.call('ZADD', KEYS[7], expireAt, ARGV[1])
redis.call('XADD', KEYS[6], 'MAXLEN', '~', 100000, '*', 'event', 'FANOUT_COMPLETED',
'fanoutId', ARGV[1], 'rootRunId', redis.call('HGET', KEYS[1], 'rootRunId'), 'state', rootState)
return {'OK', rootState, tostring(expireAt)}
end
return {'OK', 'WAITING_CHILDREN', tostring(terminal)}