Update tg issues

This commit is contained in:
xCyanGrizzly
2026-03-16 16:51:30 +01:00
parent 2763de2711
commit d7bbb7587e
13 changed files with 450 additions and 70 deletions

View File

@@ -9,6 +9,7 @@ const log = childLogger("scheduler");
let running = false;
let timer: ReturnType<typeof setTimeout> | null = null;
let cycleCount = 0;
let activeCyclePromise: Promise<void> | null = null;
/**
* Maximum time for a single ingestion cycle (ms).
@@ -107,7 +108,9 @@ function scheduleNext(): void {
);
timer = setTimeout(async () => {
await runCycle();
activeCyclePromise = runCycle();
await activeCyclePromise;
activeCyclePromise = null;
scheduleNext();
}, delay);
}
@@ -125,7 +128,9 @@ export async function startScheduler(): Promise<void> {
);
// Run immediately on start
await runCycle();
activeCyclePromise = runCycle();
await activeCyclePromise;
activeCyclePromise = null;
// Then schedule recurring cycles
scheduleNext();
@@ -146,11 +151,21 @@ export async function triggerImmediateCycle(): Promise<void> {
/**
* Stop the scheduler gracefully.
* Returns a promise that resolves when any active cycle finishes,
* so callers can wait before closing DB connections.
*/
export function stopScheduler(): void {
export function stopScheduler(): Promise<void> {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (activeCyclePromise) {
log.info("Scheduler stopping — waiting for active cycle to finish");
return activeCyclePromise.finally(() => {
activeCyclePromise = null;
log.info("Scheduler stopped");
});
}
log.info("Scheduler stopped");
return Promise.resolve();
}