if redis.call('EXISTS', KEYS[1]) == 1 then
if redis.call('HGET', KEYS[1], 'snapshotId') == ARGV[4] then return {'ADOPTED'} end
return {'CONFLICT'}
end
local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
local nextWatchAt = now + tonumber(ARGV[14])
redis.call('HSET', KEYS[1],
'schedulerQualifier', ARGV[17],
'fanoutId', ARGV[1], 'rootRunId', ARGV[2], 'rootAttempt', ARGV[3],
'snapshotId', ARGV[4], 'snapshotPayload', ARGV[5], 'workerName', ARGV[6],
'contractRevision', ARGV[7], 'schemaId', ARGV[8], 'wireCodec', ARGV[9],
'shardTotal', ARGV[10], 'createdShardCount', 0, 'deliveryCursor', 0,
'fanoutReceiptTimeoutMs', ARGV[11], 'fanoutReceiptMaxRetries', ARGV[12],
'failurePolicy', ARGV[13], 'state', 'CREATING', 'createdAt', now,
'createDeadlineAt', nextWatchAt, 'nextWatchAt', nextWatchAt,
'successCount', 0, 'failedCount', 0,
'deadCount', 0, 'skippedCount', 0, 'cancelledCount', 0,
'rootJobName', ARGV[15], 'rootScheduleShard', ARGV[16])
redis.call('ZADD', KEYS[2], nextWatchAt, ARGV[1])
return {'OK', tostring(now)}