Files
SpigotWatch/util/queue.js
2026-02-25 18:51:13 +01:00

58 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* util/queue.js
* Verarbeitet Update-Checks als geordnete Warteschlange
* verhindert parallele API-Anfragen und Rate-Limits.
*/
export class UpdateQueue {
constructor() {
this._queue = [];
this._running = false;
this.processed = 0; // Gesamt-Jobs seit Start
this.nextRunAt = null; // Zeitstempel des nächsten geplanten Runs
}
/** Anzahl der wartenden Jobs */
get size() {
return this._queue.length;
}
/** Ist die Queue gerade aktiv? */
get isRunning() {
return this._running;
}
/**
* Fügt einen asynchronen Job zur Queue hinzu.
* @param {() => Promise<void>} job
*/
enqueue(job) {
this._queue.push(job);
this._run();
}
/** Interne Abarbeitungsschleife läuft bis Queue leer ist */
async _run() {
if (this._running) return;
this._running = true;
while (this._queue.length > 0) {
const job = this._queue.shift();
try {
await job();
} catch (e) {
console.error("[Queue] Job-Fehler:", e.message);
}
this.processed++;
// 500ms Pause zwischen Jobs
if (this._queue.length > 0) {
await new Promise((r) => setTimeout(r, 500));
}
}
this._running = false;
}
}
export default new UpdateQueue();