Skip to content

Commit b4af9b3

Browse files
committed
fix: start previously scheduled tasks when concurrency limit increases
1 parent 538a940 commit b4af9b3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/ProcessingPriorityQueue.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default class ProcessingPriorityQueue {
4747

4848
public setConcurrencyLimit(concurrencyLimit: number): void {
4949
this.concurrencyLimit = concurrencyLimit;
50+
this.process()
5051
}
5152

5253
public async enqueue(ptask: PTask<any, any>): Promise<any> {

tests/unit/PTask.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -721,4 +721,42 @@ describe("PriorityTask", () => {
721721
});
722722
}, 10);
723723
});
724+
725+
it("should start previously schedules tasks when concurrencyLimit is increased", (done) => {
726+
const CONCURRENCY_LIMIT = 2;
727+
const QUEUE_NAME = 'pokeymans'
728+
PTask.setConcurrencyLimit(CONCURRENCY_LIMIT, QUEUE_NAME);
729+
730+
const delayedOnRun = async (a: number) => {
731+
await new Promise((resolve) => setTimeout(resolve, 100));
732+
return a;
733+
};
734+
735+
// Prepare tasks
736+
const ptasks = Array.from({ length: CONCURRENCY_LIMIT + 1 }).map((_, i) => {
737+
return new PTask<number, number>({
738+
priority: i,
739+
args: i,
740+
onRun: delayedOnRun,
741+
queueName: QUEUE_NAME,
742+
});
743+
});
744+
745+
// run all tasks
746+
const promises = ptasks.map((ptask) => ptask.run());
747+
748+
setTimeout(() => {
749+
PTask.setConcurrencyLimit(CONCURRENCY_LIMIT + 1, QUEUE_NAME);
750+
751+
const runningTasks = PTask.getAllPTasks(QUEUE_NAME).filter((ptask) => ptask.status === "running");
752+
const pendingTasks = PTask.getAllPTasks(QUEUE_NAME).filter((ptask) => ptask.status === "pending");
753+
754+
expect(runningTasks.length).toBe(CONCURRENCY_LIMIT + 1);
755+
expect(pendingTasks.length).toBe(0);
756+
757+
Promise.all(promises).then(() => {
758+
done()
759+
});
760+
}, 10);
761+
});
724762
});

0 commit comments

Comments
 (0)