Skip to content

Commit

Permalink
feat(queue): add getCountsPerPriority method (#2595)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Jun 11, 2024
1 parent a5a0af8 commit 77971f4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/classes/queue-getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ export class QueueGetters<
return this.getJobCountByTypes('prioritized');
}

/**
* Returns the number of jobs per priority.
*/
async getCountsPerPriority(priorities: number[]): Promise<{
[index: string]: number;
}> {
const uniquePriorities = [...new Set(priorities)];
const responses = await this.scripts.getCountsPerPriority(uniquePriorities);

const counts: { [index: string]: number } = {};
responses.forEach((res, index) => {
counts[`${uniquePriorities[index]}`] = res || 0;
});

return counts;
}

/**
* Returns the number of jobs in waiting or paused statuses.
*/
Expand Down
18 changes: 18 additions & 0 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,24 @@ export class Scripts {
return (<any>client).getCounts(args);
}

private getCountsPerPriorityArgs(priorities: number[]): (string | number)[] {
const keys: (string | number)[] = [
this.queue.keys.wait,
this.queue.keys.prioritized,
];

const args = priorities;

return keys.concat(args);
}

async getCountsPerPriority(priorities: number[]): Promise<number[]> {
const client = await this.queue.client;
const args = this.getCountsPerPriorityArgs(priorities);

return (<any>client).getCountsPerPriority(args);
}

moveToCompletedArgs<T = any, R = any, N extends string = string>(
job: MinimalJob<T, R, N>,
returnvalue: R,
Expand Down
25 changes: 25 additions & 0 deletions src/commands/getCountsPerPriority-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--[[
Get counts per provided states
Input:
KEYS[1] wait key
KEYS[2] prioritized 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)
else
results[#results+1] = rcall("ZCOUNT", prioritizedKey,
priority * 0x100000000, (priority + 1) * 0x100000000 - 1)
end
end

return results
24 changes: 24 additions & 0 deletions tests/test_getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,30 @@ describe('Jobs getters', function () {
});
});

describe('.getCountsPerPriority', () => {
it('returns job counts per priority', async () => {
await queue.waitUntilReady();

const jobs = Array.from(Array(42).keys()).map(index => ({
name: 'test',
data: {},
opts: {
priority: index % 4,
},
}));
await queue.addBulk(jobs);

const counts = await queue.getCountsPerPriority([0, 1, 2, 3]);

expect(counts).to.be.eql({
'0': 11,
'1': 11,
'2': 10,
'3': 10,
});
});
});

describe('.getDependencies', () => {
it('return unprocessed jobs that are dependencies of a given parent job', async () => {
const flowProducer = new FlowProducer({ connection, prefix });
Expand Down

0 comments on commit 77971f4

Please sign in to comment.