Review funkt
This commit is contained in:
123
node_modules/bullmq/dist/esm/scripts/changePriority-7.js
generated
vendored
Normal file
123
node_modules/bullmq/dist/esm/scripts/changePriority-7.js
generated
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
const content = `--[[
|
||||
Change job priority
|
||||
Input:
|
||||
KEYS[1] 'wait',
|
||||
KEYS[2] 'paused'
|
||||
KEYS[3] 'meta'
|
||||
KEYS[4] 'prioritized'
|
||||
KEYS[5] 'active'
|
||||
KEYS[6] 'pc' priority counter
|
||||
KEYS[7] 'marker'
|
||||
ARGV[1] priority value
|
||||
ARGV[2] prefix key
|
||||
ARGV[3] job id
|
||||
ARGV[4] lifo
|
||||
Output:
|
||||
0 - OK
|
||||
-1 - Missing job
|
||||
]]
|
||||
local jobId = ARGV[3]
|
||||
local jobKey = ARGV[2] .. jobId
|
||||
local priority = tonumber(ARGV[1])
|
||||
local rcall = redis.call
|
||||
-- Includes
|
||||
--[[
|
||||
Function to add job in target list and add marker if needed.
|
||||
]]
|
||||
-- Includes
|
||||
--[[
|
||||
Add marker if needed when a job is available.
|
||||
]]
|
||||
local function addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
|
||||
if not isPausedOrMaxed then
|
||||
rcall("ZADD", markerKey, 0, "0")
|
||||
end
|
||||
end
|
||||
local function addJobInTargetList(targetKey, markerKey, pushCmd, isPausedOrMaxed, jobId)
|
||||
rcall(pushCmd, targetKey, jobId)
|
||||
addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
|
||||
end
|
||||
--[[
|
||||
Function to add job considering priority.
|
||||
]]
|
||||
-- Includes
|
||||
--[[
|
||||
Function to get priority score.
|
||||
]]
|
||||
local function getPriorityScore(priority, priorityCounterKey)
|
||||
local prioCounter = rcall("INCR", priorityCounterKey)
|
||||
return priority * 0x100000000 + prioCounter % 0x100000000
|
||||
end
|
||||
local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounterKey,
|
||||
isPausedOrMaxed)
|
||||
local score = getPriorityScore(priority, priorityCounterKey)
|
||||
rcall("ZADD", prioritizedKey, score, jobId)
|
||||
addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
|
||||
end
|
||||
--[[
|
||||
Function to check if queue is paused or maxed
|
||||
(since an empty list and !EXISTS are not really the same).
|
||||
]]
|
||||
local function isQueuePausedOrMaxed(queueMetaKey, activeKey)
|
||||
local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency")
|
||||
if queueAttributes[1] then
|
||||
return true
|
||||
else
|
||||
if queueAttributes[2] then
|
||||
local activeCount = rcall("LLEN", activeKey)
|
||||
return activeCount >= tonumber(queueAttributes[2])
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
--[[
|
||||
Function to push back job considering priority in front of same prioritized jobs.
|
||||
]]
|
||||
local function pushBackJobWithPriority(prioritizedKey, priority, jobId)
|
||||
-- in order to put it at front of same prioritized jobs
|
||||
-- we consider prioritized counter as 0
|
||||
local score = priority * 0x100000000
|
||||
rcall("ZADD", prioritizedKey, score, jobId)
|
||||
end
|
||||
local function reAddJobWithNewPriority( prioritizedKey, markerKey, waitKey,
|
||||
priorityCounter, lifo, priority, jobId, isPausedOrMaxed)
|
||||
if priority == 0 then
|
||||
local pushCmd = lifo and 'RPUSH' or 'LPUSH'
|
||||
addJobInTargetList(waitKey, markerKey, pushCmd, isPausedOrMaxed, jobId)
|
||||
else
|
||||
if lifo then
|
||||
pushBackJobWithPriority(prioritizedKey, priority, jobId)
|
||||
else
|
||||
addJobWithPriority(markerKey, prioritizedKey, priority, jobId,
|
||||
priorityCounter, isPausedOrMaxed)
|
||||
end
|
||||
end
|
||||
end
|
||||
if rcall("EXISTS", jobKey) == 1 then
|
||||
local metaKey = KEYS[3]
|
||||
local activeKey = KEYS[5]
|
||||
local waitKey = KEYS[1]
|
||||
local pausedKey = KEYS[2]
|
||||
local isPausedOrMaxed = isQueuePausedOrMaxed(metaKey, activeKey)
|
||||
local prioritizedKey = KEYS[4]
|
||||
local priorityCounterKey = KEYS[6]
|
||||
local markerKey = KEYS[7]
|
||||
-- Re-add with the new priority
|
||||
if rcall("ZREM", prioritizedKey, jobId) > 0 then
|
||||
reAddJobWithNewPriority( prioritizedKey, markerKey, waitKey,
|
||||
priorityCounterKey, ARGV[4] == '1', priority, jobId, isPausedOrMaxed)
|
||||
elseif rcall("LREM", waitKey, -1, jobId) > 0 then
|
||||
reAddJobWithNewPriority( prioritizedKey, markerKey, waitKey,
|
||||
priorityCounterKey, ARGV[4] == '1', priority, jobId, isPausedOrMaxed)
|
||||
end
|
||||
rcall("HSET", jobKey, "priority", priority)
|
||||
return 0
|
||||
else
|
||||
return -1
|
||||
end
|
||||
`;
|
||||
export const changePriority = {
|
||||
name: 'changePriority',
|
||||
content,
|
||||
keys: 7,
|
||||
};
|
||||
Reference in New Issue
Block a user