import { priorityQueue, type AsyncPriorityQueue } from 'async'; export class TaskProcessor { private maxParallelTasks: number; private taskQueue: AsyncPriorityQueue<() => Promise>; constructor(maxParallelTasks: number) { this.maxParallelTasks = maxParallelTasks; this.taskQueue = priorityQueue(function (task, callback) { task().then(() => callback(), (e) => callback(e)); }, this.maxParallelTasks); this.taskQueue.error((e) => { console.log("Error in task queue task: " + e); }) console.log(`Task processor created with a maximum of ${maxParallelTasks} parallel tasks`); } schedule(task: () => Promise, priority = 100): Promise { return new Promise((resolve) => { this.taskQueue.buffer this.taskQueue.push(task, priority, () => resolve()); }) } unsaturated(): Promise { return this.taskQueue.unsaturated(); } }