A Bluesky labeler that labels accounts hosted on PDSes operated by entities other than Bluesky PBC
1import { priorityQueue, type AsyncPriorityQueue } from 'async';
2import { withTimeout } from './utils.js';
3
4export class TaskProcessor {
5 private maxParallelTasks: number;
6 private taskQueue: AsyncPriorityQueue<() => Promise<void>>;
7
8 constructor(maxParallelTasks: number) {
9 this.maxParallelTasks = maxParallelTasks;
10 this.taskQueue = priorityQueue(function (task, callback) {
11 task().then(() => callback(), (e) => callback(e));
12 }, this.maxParallelTasks);
13 this.taskQueue.error((e) => {
14 console.log("Error in task queue task: " + e);
15 })
16 console.log(`Task processor created with a maximum of ${maxParallelTasks} parallel tasks`);
17 }
18
19 schedule(task: () => Promise<any>, timeoutMs: number, priority = 100): Promise<void> {
20 return new Promise((resolve, reject) => {
21 const wrappedTask = () => withTimeout(task(), timeoutMs);
22 this.taskQueue.push(wrappedTask, priority, (err) => {
23 if (err) {
24 reject(err);
25 } else {
26 resolve();
27 }
28 });
29 })
30 }
31
32 unsaturated(): Promise<void> {
33 return this.taskQueue.unsaturated();
34 }
35}