Skip to content

Commit

Permalink
Fix transaction error handling (#973)
Browse files Browse the repository at this point in the history
* fix exec error handling

* lint

---------

Co-authored-by: Evan Tahler <evan@evantahler.com>
  • Loading branch information
itleigns and evantahler committed Aug 7, 2023
1 parent c703c00 commit 8e3eb81
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
50 changes: 44 additions & 6 deletions src/core/queue.ts
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
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
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

0 comments on commit 8e3eb81

Please sign in to comment.