Skip to content

Commit

Permalink
fix: make sure to remove failed items from queue
Browse files Browse the repository at this point in the history
  • Loading branch information
mohasarc committed Dec 10, 2024
1 parent e7a5edf commit 06502ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/PTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ export class PTask<T, R> {
}

public async run(): Promise<R> {
const newRes = await ProcessingPriorityQueue.getInstance(
this.queueName
).enqueue(this);
const result = this.resultsMerge(this.resSoFar, newRes);
this.removeSelfFromQueue();
this._status = "completed";
return result;
try {
const newRes = await ProcessingPriorityQueue.getInstance(
this.queueName
).enqueue(this);

return this.resultsMerge(this.resSoFar, newRes);
} finally {
this.removeSelfFromQueue();
this._status = "completed";
}
}

public async pause(): Promise<void> {
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/PTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe("PriorityTask", () => {
});
});

it("should be removed from the queue when complete", (done) => {
it("should be removed from the queue when complete successfully", (done) => {
const ptask = new PTask<void, void>({
args: undefined,
priority: 1,
Expand All @@ -685,6 +685,23 @@ describe("PriorityTask", () => {
});
expect(PTask.getAllPTasks("krombopulos").length).toBe(1);
});

it('should be removed from the queue when complete unsuccessfully', (done) => {
const ptask = new PTask<void, void>({
args: undefined,
priority: 1,
onRun: async () =>
await new Promise((resolve, reject) => setTimeout(reject, 500)),
queueName: "krombopulos-2",
});

ptask.run()
.catch(() => {
expect(PTask.getAllPTasks("krombopulos-2").length).toBe(0);
done();
});
expect(PTask.getAllPTasks("krombopulos-2").length).toBe(1);
})

it("should not schedule more than concurrencyLimit items", (done) => {
const CONCURRENCY_LIMIT = 2;
Expand Down

0 comments on commit 06502ec

Please sign in to comment.