if redis.call('HGET', KEYS[1], 'state') ~= 'RUNNING' then
redis.call('ZREM', KEYS[2], ARGV[1])
return {'STALE'}
end
local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
local wakeDelayMs = -1
local leaseUntil = tonumber(redis.call('HGET', KEYS[1], 'leaseUntil') or '0')
local score = tonumber(redis.call('ZSCORE', KEYS[2], ARGV[1]) or '0')
if leaseUntil ~= score or leaseUntil > now then return {'NOT_DUE', tostring(leaseUntil)} end
if ARGV[6] == 'FANOUT' or ARGV[6] == 'FANOUT_CANCEL_ONLY' then
local rootState = redis.call('HGET', KEYS[4], 'state')
if ARGV[6] == 'FANOUT_CANCEL_ONLY' and rootState ~= 'CANCELLING' then
return {'ROOT_NOT_CANCELLING'}
end
redis.call('ZREM', KEYS[2], ARGV[1])
redis.call('ZREM', KEYS[3], ARGV[1])
redis.call('ZREM', KEYS[5], ARGV[1])
local attempt = tonumber(redis.call('HGET', KEYS[1], 'attempt') or '0')
if rootState == 'CANCELLING' then
redis.call('HSET', KEYS[1], 'state', 'CANCELLED', 'owner', '', 'leaseUntil', '',
'finishedAt', now, 'resultCode', 'CANCELLED', 'errorType', 'ROOT_CANCELLING')
redis.call('HINCRBY', KEYS[4], 'cancelledCount', 1)
elseif attempt < tonumber(ARGV[3]) and ARGV[9] == 'REASSIGN_ON_FAILURE' then
redis.call('HSET', KEYS[1], 'state', 'AWAITING_CAPABILITY', 'owner', '', 'leaseUntil', '',
'errorType', 'LEASE_EXPIRED')
redis.call('ZADD', KEYS[6], now, ARGV[7])
return {'OK', 'AWAITING_CAPABILITY', tostring(now)}
elseif attempt < tonumber(ARGV[3]) then
local startVisibleAt = now + tonumber(ARGV[4])
redis.call('HSET', KEYS[1], 'state', 'RECEIVED', 'owner', '', 'leaseUntil', '',
'errorType', 'LEASE_EXPIRED', 'startVisibleAt', startVisibleAt)
redis.call('ZADD', KEYS[3], startVisibleAt, ARGV[1])
return {'OK', 'RECEIVED', tostring(now)}
else
redis.call('HSET', KEYS[1], 'state', 'DEAD', 'owner', '', 'leaseUntil', '',
'finishedAt', now, 'resultCode', 'LEASE_EXPIRED', 'errorType', 'LEASE_EXPIRED')
redis.call('HINCRBY', KEYS[4], 'deadCount', 1)
end
local terminal = tonumber(redis.call('HGET', KEYS[4], 'successCount') or '0')
+ tonumber(redis.call('HGET', KEYS[4], 'failedCount') or '0')
+ tonumber(redis.call('HGET', KEYS[4], 'deadCount') or '0')
+ tonumber(redis.call('HGET', KEYS[4], 'skippedCount') or '0')
+ tonumber(redis.call('HGET', KEYS[4], 'cancelledCount') or '0')
local total = tonumber(redis.call('HGET', KEYS[4], 'shardTotal') or '0')
if terminal == total then
local failed = tonumber(redis.call('HGET', KEYS[4], 'failedCount') or '0')
+ tonumber(redis.call('HGET', KEYS[4], 'deadCount') or '0')
local skipped = tonumber(redis.call('HGET', KEYS[4], 'skippedCount') or '0')
local cancelled = tonumber(redis.call('HGET', KEYS[4], 'cancelledCount') or '0')
local cancelling = redis.call('HGET', KEYS[4], 'state') == 'CANCELLING'
local completedState = cancelling and 'CANCELLED' or (failed > 0 and 'FAILED'
or (skipped > 0 and 'PARTIAL_FAILED'
or (cancelled == total and 'CANCELLED' or 'SUCCEEDED')))
local expireAt = now + tonumber(ARGV[5])
redis.call('HSET', KEYS[4], 'state', completedState, 'finishedAt', now, 'expireAt', expireAt)
redis.call('ZADD', KEYS[8], expireAt, ARGV[7])
redis.call('XADD', KEYS[7], 'MAXLEN', '~', 100000, '*', 'event', 'FANOUT_COMPLETED',
'fanoutId', ARGV[7], 'rootRunId', redis.call('HGET', KEYS[4], 'rootRunId'),
'state', completedState)
end
return {'OK', redis.call('HGET', KEYS[1], 'state'), tostring(now)}
end
redis.call('ZREM', KEYS[2], ARGV[1])
local releasedSlot = false
if redis.call('HGET', KEYS[4], ARGV[2]) == ARGV[1] then
redis.call('HDEL', KEYS[4], ARGV[2])
releasedSlot = true
end
local attempt = tonumber(redis.call('HGET', KEYS[1], 'attempt') or '0')
local jobState = redis.call('HGET', KEYS[8], 'state')
local deletedRevision = tonumber(redis.call('HGET', KEYS[8], 'deletedRevision') or '-1')
local runRevision = tonumber(redis.call('HGET', KEYS[1], 'definitionRevision') or '0')
local deletedByFence = jobState == 'DELETED' or runRevision <= deletedRevision
local nextState = deletedByFence and 'CANCELLED'
or (attempt < tonumber(ARGV[3]) and 'RETRY_WAIT' or 'DEAD')
redis.call('HSET', KEYS[1], 'state', nextState, 'owner', '', 'leaseUntil', '', 'errorType', 'LEASE_EXPIRED')
if nextState == 'RETRY_WAIT' then
local nextVisibleAt = now + tonumber(ARGV[4])
redis.call('HSET', KEYS[1], 'nextVisibleAt', nextVisibleAt)
redis.call('ZADD', KEYS[3], nextVisibleAt, ARGV[1])
wakeDelayMs = tonumber(ARGV[4])
else
redis.call('ZREM', KEYS[3], ARGV[1])
redis.call('ZREM', KEYS[5], ARGV[1])
redis.call('ZREM', KEYS[6], ARGV[1])
local terminalResult = deletedByFence and 'JOB_DELETED' or 'LEASE_EXPIRED'
redis.call('HSET', KEYS[1], 'finishedAt', now, 'resultCode', terminalResult)
redis.call('XADD', KEYS[7], 'MAXLEN', '~', 100000, '*',
'runId', ARGV[1], 'state', nextState, 'resultCode', terminalResult)
redis.call('PEXPIRE', KEYS[1], ARGV[5])
if redis.call('HGET', KEYS[1], 'triggerType') == 'FIXED_DELAY'
and redis.call('HGET', KEYS[8], 'state') == 'ENABLED'
and redis.call('HGET', KEYS[8], 'scheduleType') == 'FIXED_DELAY'
and redis.call('HGET', KEYS[1], 'definitionRevision') == redis.call('HGET', KEYS[8], 'definitionRevision') then
local nextFireAt = now + tonumber(redis.call('HGET', KEYS[8], 'intervalMs') or '0')
redis.call('HSET', KEYS[8], 'lastFireAt', redis.call('HGET', KEYS[1], 'logicalFireAt'),
'nextFireAt', nextFireAt)
redis.call('ZADD', KEYS[9], nextFireAt, ARGV[2])
end
end
if releasedSlot then
local prefix = ARGV[10]
local handoffComplete = false
for index = 1, 100 do
local nextEntry = redis.call('ZPOPMIN', KEYS[5], 1)
if not nextEntry[1] then
handoffComplete = true
break
end
local nextRunKey = prefix .. 'run:' .. nextEntry[1]
if redis.call('HGET', nextRunKey, 'state') == 'BLOCKED' then
local nextRevision = tonumber(redis.call('HGET', nextRunKey, 'definitionRevision') or '0')
if jobState == 'DELETED' or nextRevision <= deletedRevision then
redis.call('HSET', nextRunKey, 'state', 'CANCELLED', 'finishedAt', now,
'resultCode', 'JOB_DELETED')
redis.call('ZREM', KEYS[2], nextEntry[1])
redis.call('ZREM', KEYS[3], nextEntry[1])
redis.call('ZREM', KEYS[6], nextEntry[1])
redis.call('XADD', KEYS[7], 'MAXLEN', '~', 100000, '*',
'runId', nextEntry[1], 'state', 'CANCELLED', 'resultCode', 'JOB_DELETED')
redis.call('PEXPIRE', nextRunKey, ARGV[5])
else
local visibleAt = now
redis.call('HSET', nextRunKey, 'state', 'QUEUED', 'nextVisibleAt', visibleAt)
redis.call('ZADD', KEYS[3], visibleAt, nextEntry[1])
wakeDelayMs = 0
handoffComplete = true
break
end
end
end
if not handoffComplete and redis.call('ZCARD', KEYS[5]) > 0 then
redis.call('SADD', prefix .. 'reaping', ARGV[2])
end
end
return {'OK', nextState, tostring(now), tostring(wakeDelayMs)}