Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 29 additions & 17 deletions packages/cloud-agents/src/server/task-run-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ return evictedIds
const ATOMIC_DEQUEUE_SCRIPT = `
local queueLength = redis.call('LLEN', KEYS[1])
local lockTtlSeconds = tonumber(ARGV[1])
local nowMs = tonumber(ARGV[2])

for _ = 1, queueLength do
local entryId = redis.call('LPOP', KEYS[1])
Expand All @@ -269,24 +270,34 @@ for _ = 1, queueLength do
local entryScope = redis.call('HGET', KEYS[4], entryId)

if rawValue and entryScope then
local acquired = redis.call(
'SET',
entryScope,
entryId,
'EX',
lockTtlSeconds,
'NX'
)

if acquired then
redis.call('HDEL', KEYS[3], entryId)
redis.call('HDEL', KEYS[4], entryId)

if redis.call('HGET', KEYS[2], entryScope) == entryId then
redis.call('HDEL', KEYS[2], entryScope)
local decoded, entry = pcall(function() return cjson.decode(rawValue) end)
local availableAt = decoded and type(entry) == 'table' and tonumber(entry.availableAt) or nil
local delayMs = availableAt and math.ceil(availableAt - nowMs) or 0

-- An active owner's lock can prevent enqueue from installing its delay
-- marker. Releasing or expiring that lock must not bypass the deadline.
if delayMs > 0 then
redis.call('SET', entryScope, 'delay:' .. entryId, 'PX', delayMs, 'NX')
else
local acquired = redis.call(
'SET',
entryScope,
entryId,
'EX',
lockTtlSeconds,
'NX'
)

if acquired then
redis.call('HDEL', KEYS[3], entryId)
redis.call('HDEL', KEYS[4], entryId)

if redis.call('HGET', KEYS[2], entryScope) == entryId then
redis.call('HDEL', KEYS[2], entryScope)
end

return rawValue
end

return rawValue
end

redis.call('RPUSH', KEYS[1], entryId)
Expand Down Expand Up @@ -626,6 +637,7 @@ export class TaskRunQueue {
TaskRunQueueKeys.Entries,
TaskRunQueueKeys.EntryScopes,
Math.ceil(TASK_TIMEOUT_MS / 1000).toString(),
Date.now().toString(),
);

if (!rawValue) {
Expand Down
Loading