wait for pending to be available to claim task

Signed-off-by: Hao Luo <howlowck@gmail.com>
This commit is contained in:
Hao Luo
2022-11-10 07:48:29 -06:00
parent 2ea0b2d634
commit 7ddab188a0
@@ -110,13 +110,26 @@ export class TaskWorker {
start() {
(async () => {
for (;;) {
await this.taskQueue.onEmpty();
await this.onReadyToClaimTask();
const task = await this.options.taskBroker.claim();
this.taskQueue.add(() => this.runOneTask(task));
}
})();
}
onReadyToClaimTask(): Promise<void> {
if (this.taskQueue.pending < this.options.concurrentTasksLimit) {
return Promise.resolve();
}
return new Promise(resolve => {
// "next" event emits when a task completes
// https://github.com/sindresorhus/p-queue#next
this.taskQueue.on('next', () => {
resolve();
});
});
}
async runOneTask(task: TaskContext) {
try {
if (task.spec.apiVersion !== 'scaffolder.backstage.io/v1beta3') {