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

perf(retry): delete props in retryJob lua script #1016

Merged
merged 3 commits into from
Jan 27, 2022
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
13 changes: 3 additions & 10 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,25 +842,18 @@ export class Job<
}

/**
* Attempts to retry the job. Only a job that has failed can be retried.
* Attempts to retry the job. Only a job that has failed or completed can be retried.
*
* @param state - completed / failed
* @returns If resolved and return code is 1, then the queue emits a waiting event
* otherwise the operation was not a success and throw the corresponding error. If the promise
* rejects, it indicates that the script failed to execute
*/
async retry(state: 'completed' | 'failed' = 'failed'): Promise<void> {
const client = await this.queue.client;

this.failedReason = null;
this.finishedOn = null;
this.processedOn = null;

await client.hdel(
this.queue.toKey(this.id),
'finishedOn',
'processedOn',
'failedReason',
);
this.returnvalue = null;

return Scripts.reprocessJob(this.queue, this, state);
}
Expand Down
8 changes: 6 additions & 2 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ export class Scripts {
static retryJobArgs<T = any, R = any, N extends string = string>(
queue: MinimalQueue,
job: Job<T, R, N>,
) {
): string[] {
const jobId = job.id;

const keys = ['active', 'wait', jobId].map(function (name) {
Expand Down Expand Up @@ -653,7 +653,11 @@ export class Scripts {
queue.toKey('wait'),
];

const args = [job.id, (job.opts.lifo ? 'R' : 'L') + 'PUSH'];
const args = [
job.id,
(job.opts.lifo ? 'R' : 'L') + 'PUSH',
state === 'failed' ? 'failedReason' : 'returnvalue',
];

const result = await (<any>client).reprocessJob(keys.concat(args));

Expand Down
4 changes: 3 additions & 1 deletion src/commands/reprocessJob-4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
KEYS[3] job state
KEYS[4] wait key

ARGV[1] job.id,
ARGV[1] job.id
ARGV[2] (job.opts.lifo ? 'R' : 'L') + 'PUSH'
ARGV[3] propVal - failedReason/returnvalue

Output:
1 means the operation was a success
Expand All @@ -20,6 +21,7 @@ if (rcall("EXISTS", KEYS[1]) == 1) then
local jobId = ARGV[1]
if (rcall("ZREM", KEYS[3], jobId) == 1) then
rcall(ARGV[2], KEYS[4], jobId)
rcall("HDEL", KEYS[1], "finishedOn", "processedOn", ARGV[3])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

probably we should set attempstMade as 0 too, what do you think @manast ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure, why would we like to reset the number of attempts here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if the job failed after the last attempt, if we retry the job, it would keep the last maximum attempt number

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

as we are not going to reset the attempstMade, this pr should be ready


-- Emit waiting event
rcall("XADD", KEYS[2], "*", "event", "waiting", "jobId", jobId);
Expand Down
51 changes: 47 additions & 4 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ describe('workers', function () {
it('process a job that returns data with a circular dependency', async () => {
const worker = new Worker(
queueName,
async job => {
async () => {
const circular = { x: {} };
circular.x = circular;
return circular;
Expand Down Expand Up @@ -1113,7 +1113,7 @@ describe('workers', function () {

const worker = new Worker(
queueName,
async job => {
async () => {
if (!failedOnce) {
throw notEvenErr;
}
Expand Down Expand Up @@ -1158,14 +1158,55 @@ describe('workers', function () {
await worker.close();
});

it('retry a job that completes', async () => {
let completedOnce = false;

const worker = new Worker(
queueName,
async () => {
if (!completedOnce) {
return 1;
}
return 2;
},
{ connection },
);
await worker.waitUntilReady();

let count = 1;
const completing = new Promise<void>((resolve, reject) => {
worker.once('completed', async (job, result) => {
try {
expect(job).to.be.ok;
expect(job.data.foo).to.be.eql('bar');
expect(result).to.be.eql(count++);
completedOnce = true;
} catch (err) {
reject(err);
}
resolve();
});
});

const job = await queue.add('test', { foo: 'bar' });
expect(job.id).to.be.ok;
expect(job.data.foo).to.be.eql('bar');

await completing;
await job.retry('completed');
await completing;

await worker.close();
});

it('retry a job that fails using job retry method', async () => {
let called = 0;
let failedOnce = false;
const notEvenErr = new Error('Not even!');

const worker = new Worker(
queueName,
async job => {
async () => {
called++;
if (called % 2 !== 0) {
throw notEvenErr;
Expand All @@ -1192,11 +1233,13 @@ describe('workers', function () {
expect(job.failedReason).to.be.null;
expect(job.processedOn).to.be.null;
expect(job.finishedOn).to.be.null;
expect(job.returnvalue).to.be.null;

const updatedJob = await queue.getJob(job.id);
expect(updatedJob.failedReason).to.be.undefined;
expect(updatedJob.processedOn).to.be.undefined;
expect(updatedJob.finishedOn).to.be.undefined;
expect(updatedJob.returnvalue).to.be.null;

await worker.resume();
});
Expand All @@ -1216,7 +1259,7 @@ describe('workers', function () {

const worker = new Worker(
queueName,
async job => {
async () => {
return delay(2000);
},
{
Expand Down