mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: add initDispatcher() for eager sidecar startup
The dispatcher was lazy-initialized on first AI request, but a race condition meant the first call always missed it (dispatcherReady still false) and fell through to cold per-request Python. initDispatcher() starts the dispatcher eagerly and returns a Promise that resolves with GPU status once ready (or after a timeout).
This commit is contained in:
@@ -330,6 +330,44 @@ export function shutdownDispatcher(): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Eagerly start the Python dispatcher and wait for its readiness signal.
|
||||
* Returns the GPU status once ready, or {ready: false} on timeout/failure.
|
||||
* Safe to call multiple times -- idempotent if the dispatcher is already running.
|
||||
*/
|
||||
export function initDispatcher(timeoutMs = 30_000): Promise<{ ready: boolean; gpu: boolean }> {
|
||||
if (dispatcherReady) {
|
||||
return Promise.resolve({ ready: true, gpu: dispatcherGpuAvailable });
|
||||
}
|
||||
if (dispatcherFailed) {
|
||||
return Promise.resolve({ ready: false, gpu: false });
|
||||
}
|
||||
|
||||
const proc = getDispatcher();
|
||||
if (!proc) {
|
||||
return Promise.resolve({ ready: false, gpu: false });
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const timer = setTimeout(() => {
|
||||
clearInterval(poll);
|
||||
resolve({ ready: false, gpu: false });
|
||||
}, timeoutMs);
|
||||
|
||||
const poll = setInterval(() => {
|
||||
if (dispatcherReady) {
|
||||
clearTimeout(timer);
|
||||
clearInterval(poll);
|
||||
resolve({ ready: true, gpu: dispatcherGpuAvailable });
|
||||
} else if (dispatcherFailed) {
|
||||
clearTimeout(timer);
|
||||
clearInterval(poll);
|
||||
resolve({ ready: false, gpu: false });
|
||||
}
|
||||
}, 50);
|
||||
});
|
||||
}
|
||||
|
||||
// ── Per-request fallback (original implementation) ──────────────────
|
||||
|
||||
function runPythonPerRequest(
|
||||
|
||||
Reference in New Issue
Block a user