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(worker): add discardTtl option #1653

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,38 @@ export class Scripts {
]);
}

private discardJobArgs(jobId: string, token: string): (string | number)[] {
const keys: (string | number)[] = [
'active',
'wait',
'paused',
jobId,
'meta',
].map(name => {
return this.queue.toKey(name);
});

keys.push(
this.queue.keys.events,
this.queue.keys.delayed,
this.queue.keys.priority,
);

return keys.concat([this.queue.toKey(''), Date.now(), jobId, token]);
}

async discardJob(jobId: string, token: string): Promise<void> {
const client = await this.queue.client;

const args = this.discardJobArgs(jobId, token);

const result = await (<any>client).discardJob(args);

if (result < 0) {
throw this.finishedErrors(result, jobId, 'addJob', 'active');
}
}

retryJobArgs(
jobId: string,
lifo: boolean,
Expand Down
45 changes: 29 additions & 16 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import { Redis } from 'ioredis';
import * as path from 'path';
import { v4 } from 'uuid';
import { ErrorMessages } from '../enums';
import {
GetNextJobOptions,
IoredisListener,
Expand Down Expand Up @@ -72,6 +73,14 @@ export interface WorkerListener<
prev: string,
) => void;

/**
* Listen to 'discarded' event.
*
* This event is triggered when a job is picked after the discardTtl.
* Job is removed.
*/
discarded: (job: Job<DataType, ResultType, NameType>, prev: string) => void;

/**
* Listen to 'drained' event.
*
Expand Down Expand Up @@ -144,8 +153,6 @@ export interface WorkerListener<
stalled: (jobId: string, prev: string) => void;
}

const RATE_LIMIT_ERROR = 'bullmq:rateLimitExceeded';

/**
*
* This class represents a worker that is able to process jobs from the queue.
Expand Down Expand Up @@ -187,7 +194,7 @@ export class Worker<
>>;

static RateLimitError(): Error {
return new Error(RATE_LIMIT_ERROR);
return new Error(ErrorMessages.RATE_LIMIT);
}

constructor(
Expand Down Expand Up @@ -536,11 +543,6 @@ export class Worker<
? Math.ceil(blockTimeout)
: blockTimeout;

// We restrict the maximum block timeout to 10 second to avoid
// blocking the connection for too long in the case of reconnections
// reference: https://github.com/taskforcesh/bullmq/issues/1658
blockTimeout = Math.min(blockTimeout, maximumBlockTimeout);

const jobId = await client.brpoplpush(
this.keys.wait,
this.keys.active,
Expand Down Expand Up @@ -632,17 +634,16 @@ export class Worker<
const handleFailed = async (err: Error) => {
if (!this.connection.closing) {
try {
if (err.message == RATE_LIMIT_ERROR) {
if (err.message == ErrorMessages.RATE_LIMIT) {
this.limitUntil = await this.moveLimitedBackToWait(job, token);
return;
}

if (
(err instanceof DelayedError || err.name == 'DelayedError') ||
(
err instanceof WaitingChildrenError ||
err.name == 'WaitingChildrenError'
)
err instanceof DelayedError ||
err.name == 'DelayedError' ||
err instanceof WaitingChildrenError ||
err.name == 'WaitingChildrenError'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about this err.name no need to also change to message ?

) {
return;
}
Expand All @@ -664,8 +665,16 @@ export class Worker<

try {
jobsInProgress.add(inProgressItem);
const result = await this.callProcessJob(job, token);
return await handleCompleted(result);
if (this.opts.discardTtl) {
if (this.opts.discardTtl < Date.now() - job.timestamp) {
await this.discardJob(job.id, token);

this.emit('discarded', job, 'active');
}
} else {
const result = await this.callProcessJob(job, token);
return await handleCompleted(result);
}
} catch (err) {
return handleFailed(<Error>err);
} finally {
Expand Down Expand Up @@ -951,4 +960,8 @@ export class Worker<
}
return parseInt(limitUntil as string) || 0;
}

private async discardJob(jobId: string, token: string) {
await this.scripts.discardJob(jobId, token);
}
}
58 changes: 58 additions & 0 deletions src/commands/discardJob-8.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--[[
Removes job from queue after ttl.

Input:
KEYS[1] 'active',
KEYS[2] 'wait'
KEYS[3] 'paused'
KEYS[4] job key
KEYS[5] 'meta'
KEYS[6] events stream
KEYS[7] delayed key
KEYS[8] priority key

ARGV[1] key prefix
ARGV[2] timestamp
ARGV[3] jobId
ARGV[4] token

Events:
'waiting'

Output:
0 - OK
-1 - Missing key
-2 - Missing lock
]]
local rcall = redis.call

-- Includes
--- @include "includes/getTargetQueueList"
--- @include "includes/promoteDelayedJobs"
--- @include "includes/removeJob"

promoteDelayedJobs(KEYS[7], KEYS[2], KEYS[8], KEYS[3], KEYS[5], KEYS[6], ARGV[1], ARGV[2])

if rcall("EXISTS", KEYS[4]) == 1 then

if ARGV[4] ~= "0" then
local lockKey = KEYS[4] .. ':lock'
if rcall("GET", lockKey) == ARGV[4] then
rcall("DEL", lockKey)
else
return -2
end
end

local target = getTargetQueueList(KEYS[5], KEYS[2], KEYS[3])

rcall("LREM", KEYS[1], 0, ARGV[3])
removeJob(ARGV[3], false, ARGV[1])

-- Emit discarded event
rcall("XADD", KEYS[6], "*", "event", "discarded", "jobId", ARGV[3], "prev", "active");

return 0
else
return -1
end
File renamed without changes.
3 changes: 3 additions & 0 deletions src/enums/error-messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum ErrorMessages {
RATE_LIMIT = 'bullmq:rateLimitExceeded',
}
3 changes: 2 additions & 1 deletion src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './error-code.enum';
export * from './error-code';
export * from './error-messages';
export * from './metrics-time';
5 changes: 5 additions & 0 deletions src/interfaces/worker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ export interface WorkerOptions extends QueueBaseOptions {
*/
runRetryDelay?: number;

/**
* Time for discard jobs without processing them.
*/
discardTtl?: number;

/**
* More advanced options.
*/
Expand Down
39 changes: 39 additions & 0 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,45 @@ describe('workers', function () {
});
});

describe('when discardTtl is provided', function () {
it('remove jobs when time is greater than discardTtl', async () => {
const maxJobs = 5;

const worker = new Worker(queueName, async () => {}, {
autorun: false,
connection,
discardTtl: 100,
});
await worker.waitUntilReady();

const jobs = Array.from(Array(maxJobs).keys()).map(index => ({
name: 'test',
data: { index },
}));

await queue.addBulk(jobs);

const failing = new Promise<void>(resolve => {
worker.on(
'discarded',
after(maxJobs, (job: Job, prev: string) => {
expect(prev).to.be.eql('active');
expect(job.name).to.be.eql('test');
resolve();
}),
);
});

await delay(150);

worker.run();

await failing;

await worker.close();
});
});

it('process a job that updates progress as number', async () => {
let processor;

Expand Down