local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
local expireAt = now + tonumber(ARGV[13])
local boundWorker = redis.call('GET', KEYS[6])
if boundWorker and boundWorker ~= ARGV[7] then return {'WORKER_KEY_CONFLICT'} end
if not boundWorker then redis.call('SET', KEYS[6], ARGV[7]) end
local fanoutCapable = ARGV[16] == '1'
if fanoutCapable then
local existingSchema = redis.call('HGET', KEYS[5], 'schemaId')
if existingSchema and (existingSchema ~= ARGV[9] or redis.call('HGET', KEYS[5], 'wireCodecs') ~= ARGV[10]) then
redis.call('HSET', KEYS[5], 'conflictState', 'CONFLICT')
return {'CONTRACT_MISMATCH'}
end
if not existingSchema then
redis.call('HSET', KEYS[5], 'workerName', ARGV[7], 'contractRevision', ARGV[8],
'schemaId', ARGV[9], 'wireCodecs', ARGV[10], 'canonicalDigest', ARGV[15],
'firstDeclaredBy', ARGV[1], 'firstDeclaredAt', now, 'conflictState', '')
end
end
local revision = redis.call('HINCRBY', KEYS[2], 'heartbeatRevision', 1)
redis.call('HSET', KEYS[2],
'executorId', ARGV[1], 'nodeIdentity', ARGV[2], 'startupId', ARGV[3],
'applicationName', ARGV[4], 'runtime', ARGV[5], 'state', ARGV[6],
'capacity', ARGV[12], 'inflight', 0, 'fanoutReady', ARGV[6] == 'ACTIVE' and 'true' or 'false',
'heartbeatAt', now, 'expireAt', expireAt, 'capabilityDigest', ARGV[14])
redis.call('ZADD', KEYS[1], expireAt, ARGV[1])
if fanoutCapable then
redis.call('HSET', KEYS[2], 'capability:' .. ARGV[7], '1')
redis.call('ZADD', KEYS[3], expireAt, ARGV[1])
redis.call('HSET', KEYS[4],
ARGV[1] .. '|nodeIdentity', ARGV[2],
ARGV[1] .. '|startupId', ARGV[3],
ARGV[1] .. '|applicationName', ARGV[4],
ARGV[1] .. '|runtime', ARGV[5],
ARGV[1] .. '|contractRevision', ARGV[8],
ARGV[1] .. '|schemaId', ARGV[9],
ARGV[1] .. '|wireCodecs', ARGV[10],
ARGV[1] .. '|implementationDigest', ARGV[11])
end
return {'OK', tostring(now), tostring(expireAt), tostring(revision)}