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(
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
export { removeBackground } from "./background-removal.js";
|
||||
export type { DispatcherStatus } from "./bridge.js";
|
||||
export { getDispatcherStatus, isGpuAvailable, shutdownDispatcher } from "./bridge.js";
|
||||
export {
|
||||
getDispatcherStatus,
|
||||
initDispatcher,
|
||||
isGpuAvailable,
|
||||
shutdownDispatcher,
|
||||
} from "./bridge.js";
|
||||
export { colorize } from "./colorization.js";
|
||||
export type { DetectFacesResult, FaceRegion } from "./face-detection.js";
|
||||
export { blurFaces, detectFaces } from "./face-detection.js";
|
||||
|
||||
Reference in New Issue
Block a user