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

Fix transaction error handling #973

Merged
merged 3 commits into from
Aug 7, 2023
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
50 changes: 44 additions & 6 deletions src/core/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ export class Queue extends EventEmitter {
const toRun = await RunPlugins(this, "beforeEnqueue", func, q, job, args);
if (toRun === false) return toRun;

await this.connection.redis
const response = await this.connection.redis
.multi()
.sadd(this.connection.key("queues"), q)
.rpush(this.connection.key("queue", q), this.encode(q, func, args))
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});

await RunPlugins(this, "afterEnqueue", func, q, job, args);
return toRun;
}
Expand Down Expand Up @@ -129,7 +135,7 @@ export class Queue extends EventEmitter {
}
}

await this.connection.redis
const response = await this.connection.redis
.multi()
.rpush(this.connection.key("delayed:" + rTimestamp), item)
.sadd(this.connection.key("timestamps:" + item), "delayed:" + rTimestamp)
Expand All @@ -139,6 +145,12 @@ export class Queue extends EventEmitter {
rTimestamp.toString(),
)
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});
}
/**
* - In ms, the number of ms to delay before this job is able to start being worked on.
Expand Down Expand Up @@ -168,11 +180,17 @@ export class Queue extends EventEmitter {
*/
async delQueue(q: string) {
const { redis } = this.connection;
await redis
const response = await redis
.multi()
.del(this.connection.key("queue", q))
.srem(this.connection.key("queues"), q)
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});
}

/**
Expand Down Expand Up @@ -230,7 +248,14 @@ export class Queue extends EventEmitter {
}
}

await pipeline.exec();
const response = await pipeline.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});

return numJobsDeleted;
}

Expand Down Expand Up @@ -258,7 +283,14 @@ export class Queue extends EventEmitter {
}
}

await pipeline.exec();
const response = await pipeline.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});

return timestamps.map((t) => parseInt(t, 10));
}

Expand Down Expand Up @@ -501,7 +533,13 @@ export class Queue extends EventEmitter {
);
}

await pipeline.exec();
const response = await pipeline.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});

return errorPayload;
}
Expand Down
8 changes: 7 additions & 1 deletion src/core/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,17 @@ export class Scheduler extends EventEmitter {
await this.watchIfPossible(this.connection.key("delayed_queue_schedule"));
const length = await this.connection.redis.llen(key);
if (length === 0) {
await this.connection.redis
const response = await this.connection.redis
.multi()
.del(key)
.zrem(this.connection.key("delayed_queue_schedule"), timestamp)
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});
}
await this.unwatchIfPossible();
}
Expand Down
26 changes: 23 additions & 3 deletions src/core/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,23 @@ export class Worker extends EventEmitter {
}

private async succeed(job: ParsedJob, duration: number) {
await this.connection.redis
const response = await this.connection.redis
.multi()
.incr(this.connection.key("stat", "processed"))
.incr(this.connection.key("stat", "processed", this.name))
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});

this.emit("success", this.queue, job, this.result, duration);
}

private async fail(err: Error, duration: number) {
await this.connection.redis
const response = await this.connection.redis
.multi()
.incr(this.connection.key("stat", "failed"))
.incr(this.connection.key("stat", "failed", this.name))
Expand All @@ -422,6 +429,13 @@ export class Worker extends EventEmitter {
JSON.stringify(this.failurePayload(err, this.job)),
)
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});

this.emit("failure", this.queue, this.job, err, duration);
}

Expand Down Expand Up @@ -512,7 +526,7 @@ export class Worker extends EventEmitter {
return;
}

await this.connection.redis
const response = await this.connection.redis
.multi()
.srem(this.connection.key("workers"), name + ":" + queues)
.del(this.connection.key("worker", "ping", name))
Expand All @@ -521,6 +535,12 @@ export class Worker extends EventEmitter {
.del(this.connection.key("stat", "failed", name))
.del(this.connection.key("stat", "processed", name))
.exec();

response.forEach((res) => {
if (res[0] !== null) {
throw res[0];
}
});
}

async checkQueues() {
Expand Down