From bf3a981a8ff9e26e163fcf44e25414d0224d1e37 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Thu, 20 Jun 2024 20:23:57 -0500 Subject: [PATCH] fix(priority): consider paused state when calling getCountsPerPriority --- lib/commands/getCountsPerPriority-2.lua | 25 ----------------- lib/commands/getCountsPerPriority-4.lua | 37 +++++++++++++++++++++++++ lib/scripts.js | 7 ++++- test/test_getters.js | 22 +++++++++++++++ 4 files changed, 65 insertions(+), 26 deletions(-) delete mode 100644 lib/commands/getCountsPerPriority-2.lua create mode 100644 lib/commands/getCountsPerPriority-4.lua diff --git a/lib/commands/getCountsPerPriority-2.lua b/lib/commands/getCountsPerPriority-2.lua deleted file mode 100644 index 32e7dd8a9..000000000 --- a/lib/commands/getCountsPerPriority-2.lua +++ /dev/null @@ -1,25 +0,0 @@ ---[[ - Get counts per provided states - - Input: - KEYS[1] wait key - KEYS[2] priority key - - ARGV[1...] priorities -]] -local rcall = redis.call -local results = {} -local waitKey = KEYS[1] -local prioritizedKey = KEYS[2] - -for i = 1, #ARGV do - local priority = tonumber(ARGV[i]) - if priority == 0 then - results[#results+1] = rcall("LLEN", waitKey) - rcall("ZCARD", prioritizedKey) - else - results[#results+1] = rcall("ZCOUNT", prioritizedKey, - priority, priority) - end -end - -return results diff --git a/lib/commands/getCountsPerPriority-4.lua b/lib/commands/getCountsPerPriority-4.lua new file mode 100644 index 000000000..48a1635d6 --- /dev/null +++ b/lib/commands/getCountsPerPriority-4.lua @@ -0,0 +1,37 @@ +--[[ + Get counts per provided states + + Input: + KEYS[1] wait key + KEYS[2] paused key + KEYS[3] meta-paused key + KEYS[4] priority key + + ARGV[1...] priorities +]] +local rcall = redis.call +local results = {} +local prioritizedKey = KEYS[4] + +-- Includes +--- @include "includes/getTargetQueueList" + +for i = 1, #ARGV do + local priority = tonumber(ARGV[i]) + if priority == 0 then + local target = getTargetQueueList(KEYS[3], KEYS[1], KEYS[2]) + local count = rcall("LLEN", target) - rcall("ZCARD", prioritizedKey) + if count < 0 then + -- considering when last waiting job is moved to active before + -- removing priority reference + results[#results+1] = 0 + else + results[#results+1] = count + end + else + results[#results+1] = rcall("ZCOUNT", prioritizedKey, + priority, priority) + end +end + +return results diff --git a/lib/scripts.js b/lib/scripts.js index 3b5172d2a..93f5bef5f 100644 --- a/lib/scripts.js +++ b/lib/scripts.js @@ -84,7 +84,12 @@ const scripts = { }, getCountsPerPriorityArgs(queue, priorities) { - const keys = [queue.keys.wait, queue.keys.priority]; + const keys = [ + queue.keys.wait, + queue.keys.paused, + queue.keys['meta-paused'], + queue.keys.priority + ]; const args = priorities; diff --git a/test/test_getters.js b/test/test_getters.js index acfde9c3d..f30c650ea 100644 --- a/test/test_getters.js +++ b/test/test_getters.js @@ -192,6 +192,28 @@ describe('Jobs getters', function() { }); }); }); + + describe('when queue is paused', () => { + it('returns job counts per priority', async () => { + await queue.pause(); + const jobsArray = Array.from(Array(42).keys()).map(index => ({ + name: 'test', + data: {}, + opts: { + priority: index % 4 + } + })); + await queue.addBulk(jobsArray); + const counts = await queue.getCountsPerPriority([0, 1, 2, 3]); + + expect(counts).to.be.eql({ + '0': 11, + '1': 11, + '2': 10, + '3': 10 + }); + }); + }); }); it('fails jobs that exceed their specified timeout', done => {