Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(queue): add getCountsPerPriority method #2595

Merged
merged 2 commits into from
Jun 11, 2024
Merged
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
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
Loading