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(job): expose priority value #2804

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ export class Job<
* An amount of milliseconds to wait until this job can be processed.
* @defaultValue 0
*/
delay: number;
delay = 0;

/**
* Ranges from 0 (highest priority) to 2 097 152 (lowest priority). Note that
* using priorities has a slight impact on performance,
* so do not use it if not required.
* @defaultValue 0
*/
priority = 0;

/**
* Timestamp when the job was created (unless overridden with job options).
Expand Down Expand Up @@ -201,6 +209,8 @@ export class Job<

this.delay = this.opts.delay;

this.priority = this.opts.priority || 0;

this.repeatJobKey = repeatJobKey;

this.timestamp = opts.timestamp ? opts.timestamp : Date.now();
Expand All @@ -214,7 +224,9 @@ export class Job<
: undefined;

this.debounceId = opts.debounce ? opts.debounce.id : undefined;
this.deduplicationId = opts.deduplication ? opts.deduplication.id : this.debounceId;
this.deduplicationId = opts.deduplication
? opts.deduplication.id
: this.debounceId;

this.toKey = queue.toKey.bind(queue);
this.setScripts();
Expand Down Expand Up @@ -840,13 +852,15 @@ export class Job<
/**
* Change job priority.
*
* @param opts - options containing priority and lifo values.
* @returns void
*/
async changePriority(opts: {
priority?: number;
lifo?: boolean;
}): Promise<void> {
await this.scripts.changePriority(this.id, opts.priority, opts.lifo);
this.priority = opts.priority || 0;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/base-job-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export interface DefaultJobOptions {
timestamp?: number;

/**
* Ranges from 1 (highest priority) to 2 097 152 (lowest priority). Note that
* Ranges from 0 (highest priority) to 2 097 152 (lowest priority). Note that
* using priorities has a slight impact on performance,
* so do not use it if not required.
* @defaultValue 0
*/
priority?: number;

Expand Down
28 changes: 17 additions & 11 deletions tests/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,11 +1053,15 @@ describe('Job', function () {
{ priority: 16 },
);

expect(job.priority).to.be.eql(16);

await job.changePriority({
priority: 0,
lifo: true,
});

expect(job.priority).to.be.eql(0);

const worker = new Worker(
queueName,
async () => {
Expand Down Expand Up @@ -1367,17 +1371,19 @@ describe('Job', function () {
const job = await queue.add(
'test',
{ foo: 'bar' },
{ repeat: {
pattern: '0 0 7 * * *'
}, },
{
repeat: {
pattern: '0 0 7 * * *',
},
},
);
const isDelayed = await job.isDelayed();
expect(isDelayed).to.be.equal(true);
await job.promote();
expect(job.delay).to.be.equal(0);

const worker = new Worker(queueName, null, { connection, prefix });

const currentJob1 = (await worker.getNextJob('token')) as Job;
expect(currentJob1).to.not.be.undefined;

Expand Down Expand Up @@ -1405,7 +1411,7 @@ describe('Job', function () {
);
const isDelayed = await job.isDelayed();
expect(isDelayed).to.be.equal(true);

await queue.add(
'test',
{ foo: 'bar' },
Expand All @@ -1417,25 +1423,25 @@ describe('Job', function () {
);
const delayedCount = await queue.getDelayedCount();
expect(delayedCount).to.be.equal(1);

await job.promote();
expect(job.delay).to.be.equal(0);

const worker = new Worker(queueName, null, { connection, prefix });
const currentJob1 = (await worker.getNextJob('token')) as Job;
expect(currentJob1).to.not.be.undefined;

await currentJob1.moveToCompleted('succeeded', 'token', true);
const completedCount = await queue.getCompletedCount();
const delayedCountAfterPromote = await queue.getDelayedCount();
expect(completedCount).to.be.equal(1);
expect(delayedCountAfterPromote).to.be.equal(1);

const completedCountAfterRestart = await queue.getCompletedCount();
const delayedCountAfterRestart = await queue.getDelayedCount();
expect(completedCountAfterRestart).to.be.equal(1);
expect(delayedCountAfterRestart).to.be.equal(1);

await queue.add(
'test',
{ foo: 'bar' },
Expand All @@ -1451,7 +1457,7 @@ describe('Job', function () {
expect(completedCountAfterReAddition).to.be.equal(1);
expect(delayedCountAfterReAddition).to.be.equal(1);
});
});
});
});

describe('when queue is paused', () => {
Expand Down
Loading