Skip to content

Commit c0ecf1b

Browse files
committed
fix: make sure to remove failed items from queue
1 parent e7a5edf commit c0ecf1b

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/PTask.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,18 @@ export class PTask<T, R> {
8686
}
8787

8888
public async run(): Promise<R> {
89-
const newRes = await ProcessingPriorityQueue.getInstance(
90-
this.queueName
91-
).enqueue(this);
92-
const result = this.resultsMerge(this.resSoFar, newRes);
93-
this.removeSelfFromQueue();
94-
this._status = "completed";
95-
return result;
89+
try {
90+
const newRes = await ProcessingPriorityQueue.getInstance(
91+
this.queueName
92+
).enqueue(this);
93+
94+
return this.resultsMerge(this.resSoFar, newRes);
95+
} finally {
96+
this.removeSelfFromQueue();
97+
if (this._status === 'running') {
98+
this._status = "completed";
99+
}
100+
}
96101
}
97102

98103
public async pause(): Promise<void> {

tests/unit/PTask.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ describe("PriorityTask", () => {
670670
});
671671
});
672672

673-
it("should be removed from the queue when complete", (done) => {
673+
it("should be removed from the queue when complete successfully", (done) => {
674674
const ptask = new PTask<void, void>({
675675
args: undefined,
676676
priority: 1,
@@ -685,6 +685,23 @@ describe("PriorityTask", () => {
685685
});
686686
expect(PTask.getAllPTasks("krombopulos").length).toBe(1);
687687
});
688+
689+
it('should be removed from the queue when complete unsuccessfully', (done) => {
690+
const ptask = new PTask<void, void>({
691+
args: undefined,
692+
priority: 1,
693+
onRun: async () =>
694+
await new Promise((resolve, reject) => setTimeout(reject, 500)),
695+
queueName: "krombopulos-2",
696+
});
697+
698+
ptask.run()
699+
.catch(() => {
700+
expect(PTask.getAllPTasks("krombopulos-2").length).toBe(0);
701+
done();
702+
});
703+
expect(PTask.getAllPTasks("krombopulos-2").length).toBe(1);
704+
})
688705

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

0 commit comments

Comments
 (0)