openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
45 lines (44 loc) • 1.47 kB
JavaScript
import { l as isGatewayRestartDrainError, s as getGatewayRestartDrainSignal, w as tryBeginGatewayIndependentRootWorkAdmission } from "./gateway-work-admission-R1IpuDim.js";
//#region src/gateway/server-idle-task.ts
/** Schedules one low-priority task, retrying until the gateway has no active request roots. */
function scheduleGatewayIdleTask(params) {
let stopped = false;
let timer = null;
let running;
const isClosing = () => stopped || params.isClosing() || getGatewayRestartDrainSignal().aborted;
const run = async () => {
if (isClosing()) return;
if (params.isBusy()) schedule(params.retryDelayMs);
else await params.run();
};
const schedule = (delayMs) => {
if (isClosing()) return;
timer = setTimeout(() => {
timer = null;
if (isClosing()) return;
const admission = params.isBusy() ? null : tryBeginGatewayIndependentRootWorkAdmission("idle-task");
if (!admission) {
schedule(params.retryDelayMs);
return;
}
running = Promise.resolve().then(() => admission.run(run)).catch((error) => {
if (!isGatewayRestartDrainError(error)) params.log.warn(`${params.errorMessage}: ${String(error)}`);
}).finally(() => {
admission.release();
running = void 0;
});
}, delayMs);
timer.unref?.();
};
schedule(params.delayMs);
return { stop: () => {
stopped = true;
if (timer) {
clearTimeout(timer);
timer = null;
}
return running;
} };
}
//#endregion
export { scheduleGatewayIdleTask as t };