Skip to content

Commit 9485ad5

Browse files
authored
feat(job): add changePriority method (#1901) ref #1899
1 parent 8f4d86a commit 9485ad5

File tree

7 files changed

+343
-7
lines changed

7 files changed

+343
-7
lines changed

docs/gitbook/guide/jobs/delayed.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,26 @@ await myQueue.add('house', { color: 'white' }, { delay: 5000 });
2020
If you want to process the job after a specific point in time, just add the time remaining to that point in time. For example, let's say you want to process the job on the third of July 2035 at 10:30:
2121

2222
```typescript
23-
const targetTime = new Date("03-07-2035 10:30");
23+
const targetTime = new Date('03-07-2035 10:30');
2424
const delay = Number(targetTime) - Number(new Date());
2525

2626
await myQueue.add('house', { color: 'white' }, { delay });
2727
```
2828

29+
## Change delay
30+
31+
If you want to change the delay after inserting a delayed job, just use **changeDelay** method. For example, let's say you want to change the delay from 2000 to 4000 milliseconds:
32+
33+
```typescript
34+
const job = await Job.create(queue, 'test', { foo: 'bar' }, { delay: 2000 });
35+
36+
await job.changeDelay(4000);
37+
```
38+
39+
{% hint style="warning" %}
40+
Take in count that your job must be into delayed state when you change the delay.
41+
{% endhint %}
42+
43+
## Read more:
44+
45+
- 💡 [Change Delay API Reference](https://api.docs.bullmq.io/classes/Job.html#changeDelay)

docs/gitbook/guide/jobs/prioritized.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Jobs can also include a priority option. Using priorities, job's processing orde
66
Adding prioritized jobs is a slower operation than the other types of jobs, with a complexity O(n) relative to the number of jobs waiting in the Queue.
77
{% endhint %}
88

9-
Note that the priorities go from 1 to MAX\_INT, whereas a lower number is always a higher priority than higher numbers.
9+
Note that the priorities go from 1 to MAX_INT, whereas a lower number is always a higher priority than higher numbers.
1010

1111
Jobs without a priority assigned will get the least priority.
1212

@@ -24,3 +24,25 @@ await myQueue.add('wall', { color: 'blue' }, { priority: 7 });
2424
```
2525

2626
If several jobs are added with the same priority value, then the jobs within that priority will be processed in FIFO (First in first out) fashion.
27+
28+
## Change priority
29+
30+
If you want to change the priority after inserting a job, just use the **changePriority** method. For example, let's say that you want to change the priority from 16 to 1:
31+
32+
```typescript
33+
const job = await Job.create(queue, 'test2', { foo: 'bar' }, { priority: 16 });
34+
35+
await job.changePriority({
36+
priority: 1,
37+
});
38+
```
39+
40+
or if you want to use lifo option:
41+
42+
```typescript
43+
const job = await Job.create(queue, 'test2', { foo: 'bar' }, { priority: 16 });
44+
45+
await job.changePriority({
46+
lifo: true,
47+
});
48+
```

src/classes/job.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,18 @@ export class Job<
738738
this.delay = delay;
739739
}
740740

741+
/**
742+
* Change job priority.
743+
*
744+
* @returns void
745+
*/
746+
async changePriority(opts: {
747+
priority?: number;
748+
lifo?: boolean;
749+
}): Promise<void> {
750+
await this.scripts.changePriority(this.id, opts.priority, opts.lifo);
751+
}
752+
741753
/**
742754
* Get this jobs children result values if any.
743755
*

src/classes/scripts.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,40 @@ export class Scripts {
527527
return keys.concat([delay, JSON.stringify(timestamp), jobId]);
528528
}
529529

530+
async changePriority(
531+
jobId: string,
532+
priority = 0,
533+
lifo = false,
534+
): Promise<void> {
535+
const client = await this.queue.client;
536+
537+
const args = this.changePriorityArgs(jobId, priority, lifo);
538+
const result = await (<any>client).changePriority(args);
539+
if (result < 0) {
540+
throw this.finishedErrors(result, jobId, 'changePriority');
541+
}
542+
}
543+
544+
private changePriorityArgs(
545+
jobId: string,
546+
priority = 0,
547+
lifo = false,
548+
): (string | number)[] {
549+
const keys: (string | number)[] = [
550+
this.queue.keys.wait,
551+
this.queue.keys.paused,
552+
this.queue.keys.meta,
553+
this.queue.keys.priority,
554+
];
555+
556+
return keys.concat([
557+
priority,
558+
this.queue.toKey(jobId),
559+
jobId,
560+
lifo ? 1 : 0,
561+
]);
562+
}
563+
530564
// Note: We have an issue here with jobs using custom job ids
531565
moveToDelayedArgs(
532566
jobId: string,

src/commands/changePriority-4.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--[[
2+
Change job priority
3+
Input:
4+
KEYS[1] 'wait',
5+
KEYS[2] 'paused'
6+
KEYS[3] 'meta'
7+
KEYS[4] 'priority'
8+
9+
ARGV[1] priority value
10+
ARGV[2] job key
11+
ARGV[3] job id
12+
ARGV[4] lifo
13+
14+
Output:
15+
0 - OK
16+
-1 - Missing job
17+
]]
18+
local jobKey = ARGV[2]
19+
local jobId = ARGV[3]
20+
local priority = tonumber(ARGV[1])
21+
local rcall = redis.call
22+
23+
-- Includes
24+
--- @include "includes/addJobWithPriority"
25+
--- @include "includes/getTargetQueueList"
26+
27+
if rcall("EXISTS", jobKey) == 1 then
28+
local target = getTargetQueueList(KEYS[3], KEYS[1], KEYS[2])
29+
30+
local numRemovedElements = rcall("LREM", target, -1, jobId)
31+
if numRemovedElements > 0 then
32+
rcall("ZREM", KEYS[4], jobId)
33+
34+
-- Standard or priority add
35+
if priority == 0 then
36+
-- LIFO or FIFO
37+
local pushCmd = ARGV[4] == '1' and 'RPUSH' or 'LPUSH';
38+
rcall(pushCmd, target, jobId)
39+
else
40+
-- Priority add
41+
addJobWithPriority(KEYS[4], priority, target, jobId)
42+
end
43+
end
44+
45+
rcall("HSET", jobKey, "priority", priority)
46+
47+
return 0
48+
else
49+
return -1
50+
end

src/commands/includes/getTargetQueueList.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
local function getTargetQueueList(queueMetaKey, waitKey, pausedKey)
77
if rcall("HEXISTS", queueMetaKey, "paused") ~= 1 then
8-
return waitKey
8+
return waitKey, false
99
else
10-
return pausedKey
10+
return pausedKey, true
1111
end
1212
end

0 commit comments

Comments
 (0)