dispatched

This commit is contained in:
NoLogicAlan committed 2026-05-06 18:27:36 +03:00
1 parent eda4a8c5a4
commit 25abbaa097
1 file changed
+46 -33
+46 -33
View File
@@ -478,50 +478,63 @@ export class Remix {
let settingsReady = false; let settingsReady = false;
/** /**
* Run async tasks with bounded concurrency and optional stagger. * Run async tasks with bounded concurrency and staggered dispatch.
* @param {Array<{guildId, channelId, recoveryData?}>} items *
* Guarantees:
* - At most `concurrency` tasks in-flight at any time.
* - Minimum `staggerMs` gap between dispatching two consecutive tasks.
* - When a task finishes and a slot opens, the next dispatch happens
* after the remaining stagger time elapses (not immediately).
*
* @param {Array} items
* @param {number} concurrency * @param {number} concurrency
* @param {number} staggerMs — delay between dispatching each task * @param {number} staggerMs
* @param {(item) => Promise} fn * @param {(item) => Promise} fn
*/ */
const runWithConcurrency = async (items, concurrency, staggerMs, fn) => { const runWithConcurrency = (items, concurrency, staggerMs, fn) => {
const errors = [];
let idx = 0; let idx = 0;
let active = 0; let active = 0;
let settled = 0; let lastDispatchTime = -Infinity;
let dispatchTimer = null;
const total = items.length; const total = items.length;
const errors = [];
if (total === 0) return Promise.resolve(errors);
return new Promise((resolve) => { return new Promise((resolve) => {
const tryNext = () => { const tryDispatch = () => {
// Clear any pending stagger timer (a faster completion may have
// made it obsolete, or the resolve path was reached).
if (dispatchTimer) { clearTimeout(dispatchTimer); dispatchTimer = null; }
while (active < concurrency && idx < total) { while (active < concurrency && idx < total) {
const item = items[idx++]; const now = Date.now();
// Stagger: delay before starting each subsequent batch item const elapsed = now - lastDispatchTime;
if (idx > 1 || active > 0) {
const stagger = staggerMs; // Enforce minimum gap between dispatches (skip for the very first).
// We don't await the stagger inside the worker — instead we if (elapsed < staggerMs) {
// schedule the next spawn after the stagger using setTimeout dispatchTimer = setTimeout(tryDispatch, staggerMs - elapsed);
// so the concurrency slot is only taken after the delay. return; // spin-wait via timer — do NOT dispatch now
setTimeout(() => runOne(item), stagger);
} else {
runOne(item);
} }
const item = items[idx++];
lastDispatchTime = Date.now();
active++;
fn(item)
.catch((e) => errors.push({ ...item, error: e.message }))
.finally(() => {
active--;
tryDispatch(); // a slot opened — try to fill it
});
} }
// All done when nothing left and nothing in flight
if (settled === total) resolve(errors); // All items dispatched and all in-flight tasks settled.
if (idx >= total && active === 0) resolve(errors);
}; };
const runOne = (item) => { // Kick off the first dispatch immediately.
active++; tryDispatch();
fn(item)
.catch((e) => errors.push({ ...item, error: e.message }))
.finally(() => {
active--;
settled++;
tryNext();
});
};
tryNext();
}); });
}; };
@@ -532,7 +545,7 @@ export class Remix {
// Build a unified join list: recovery sessions + 24/7 channels // Build a unified join list: recovery sessions + 24/7 channels
const joinList = []; const joinList = [];
// Recover standard reboot sessions // 1. Recover standard reboot sessions
if (fs.existsSync(recoveryPath)) { if (fs.existsSync(recoveryPath)) {
logger.recovery("[Recovery] Found previous session data, restoring..."); logger.recovery("[Recovery] Found previous session data, restoring...");
let data = null; let data = null;
@@ -553,7 +566,7 @@ export class Remix {
} }
} }
// Collect 24/7 channels (skip channels already covered by recovery) // 2. Collect 24/7 channels (skip channels already covered by recovery)
const recoveryChannels = new Set(joinList.map(j => j.channelId)); const recoveryChannels = new Set(joinList.map(j => j.channelId));
for (const [guildId, serverSettings] of this.settingsMgr.guilds) { for (const [guildId, serverSettings] of this.settingsMgr.guilds) {
const raw = serverSettings.get("stay_247"); const raw = serverSettings.get("stay_247");