Something went wrong. Try again.
A Bluesky labeler that labels accounts hosted on PDSes operated by entities other than Bluesky PBC forked from gbl08ma.com/pdslabeler
Something went wrong. Try again.
pdslabeler taskProcessor.ts
1234567891011121314151617181920212223242526272829303132333435import { priorityQueue, type AsyncPriorityQueue } from 'async';import { withTimeout } from './utils.js';
export class TaskProcessor { private maxParallelTasks: number; private taskQueue: AsyncPriorityQueue<() => Promise<void>>;
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<any>, timeoutMs: number, priority = 100): Promise<void> { return new Promise((resolve, reject) => { const wrappedTask = () => withTimeout(task(), timeoutMs); this.taskQueue.push(wrappedTask, priority, (err) => { if (err) { reject(err); } else { resolve(); } }); }) }
unsaturated(): Promise<void> { return this.taskQueue.unsaturated(); }}