-- promote_due.lua -- -- scheduler loop op: move due tasks from the ZSET queue into the stream. -- run periodically (every ~500ms in v0) from each worker. -- -- KEYS: -- [1] queue_key — {prefix}:queue (sorted set, score = when_micros) -- [2] stream_key — {prefix}:stream -- -- ARGV: -- [1] now_micros — unix-epoch microseconds; tasks with score <= this are due -- [2] prefix — docket key prefix (for resolving parked + runs hashes per task) -- -- for each due task: -- - HGETALL the parked hash -- - XADD the message body onto the stream -- - DEL the parked hash -- - update runs hash state → 'queued', record stream_id -- then ZREMRANGEBYSCORE to clear the promoted entries from the queue. -- -- returns: { total_in_queue, num_promoted } local queue_key = KEYS[1] local stream_key = KEYS[2] local now_micros = ARGV[1] local prefix = ARGV[2] local total = redis.call('ZCARD', queue_key) local promoted = 0 if total > 0 then local due = redis.call('ZRANGEBYSCORE', queue_key, 0, now_micros) for _, task_key in ipairs(due) do local parked_key = prefix .. ':parked:' .. task_key local fields = redis.call('HGETALL', parked_key) if #fields > 0 then -- HGETALL returns a flat list [k1, v1, k2, v2, ...] — XADD takes the same shape. local msg_id = redis.call('XADD', stream_key, '*', unpack(fields)) redis.call('DEL', parked_key) local runs_key = prefix .. ':runs:' .. task_key redis.call('HSET', runs_key, 'state', 'queued', 'stream_id', msg_id ) promoted = promoted + 1 end end if promoted > 0 then redis.call('ZREMRANGEBYSCORE', queue_key, 0, now_micros) end end return { total, promoted }