perf: run provider health checks in background, notify clients on ready
The ProviderHealth layer blocked server startup with two sequential CLI spawns (codex --version + codex login status), each with a 4-second timeout, delaying startup by up to 8 seconds. Run health checks in the background via Effect.runPromise so the layer resolves immediately with a placeholder status. Add an onReady callback to ProviderHealthShape so wsServer can push the resolved statuses to connected clients once checks complete, preventing early-connecting clients from showing "Checking..." indefinitely.
This commit is contained in:
1 parent
296eb82c31
commit
6593645c80
3 files changed
+69
-5
No files matched your search
@@ -312,9 +312,56 @@ export const checkCodexProviderStatus: Effect.Effect<
|
||||
export const ProviderHealthLive = Layer.effect(
|
||||
ProviderHealth,
|
||||
Effect.gen(function* () {
|
||||
const codexStatus = yield* checkCodexProviderStatus;
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
let cachedStatuses: ReadonlyArray<ServerProviderStatus> = [
|
||||
{
|
||||
provider: CODEX_PROVIDER,
|
||||
status: "warning",
|
||||
available: false,
|
||||
authStatus: "unknown",
|
||||
checkedAt: new Date().toISOString(),
|
||||
message: "Checking Codex CLI availability...",
|
||||
},
|
||||
];
|
||||
|
||||
let readyListeners: Array<(statuses: ReadonlyArray<ServerProviderStatus>) => void> = [];
|
||||
let resolved = false;
|
||||
|
||||
const notifyReady = (statuses: ReadonlyArray<ServerProviderStatus>) => {
|
||||
resolved = true;
|
||||
cachedStatuses = statuses;
|
||||
for (const cb of readyListeners) cb(statuses);
|
||||
readyListeners = [];
|
||||
};
|
||||
|
||||
// Run health checks in the background so they don't block server startup.
|
||||
checkCodexProviderStatus.pipe(
|
||||
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
||||
Effect.runPromise,
|
||||
).then((status) => {
|
||||
notifyReady([status]);
|
||||
}).catch(() => {
|
||||
notifyReady([
|
||||
{
|
||||
provider: CODEX_PROVIDER,
|
||||
status: "error",
|
||||
available: false,
|
||||
authStatus: "unknown",
|
||||
checkedAt: new Date().toISOString(),
|
||||
message: "Failed to check Codex CLI status.",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
return {
|
||||
getStatuses: Effect.succeed([codexStatus]),
|
||||
getStatuses: Effect.sync(() => cachedStatuses),
|
||||
onReady: (cb) => {
|
||||
if (resolved) {
|
||||
cb(cachedStatuses);
|
||||
} else {
|
||||
readyListeners.push(cb);
|
||||
}
|
||||
},
|
||||
} satisfies ProviderHealthShape;
|
||||
}),
|
||||
);
|
||||
@@ -15,6 +15,10 @@ export interface ProviderHealthShape {
|
||||
* Read provider health statuses computed at server startup.
|
||||
*/
|
||||
readonly getStatuses: Effect.Effect<ReadonlyArray<ServerProviderStatus>>;
|
||||
/**
|
||||
* Register a callback invoked once when background health checks complete.
|
||||
*/
|
||||
readonly onReady: (cb: (statuses: ReadonlyArray<ServerProviderStatus>) => void) => void;
|
||||
}
|
||||
|
||||
export class ProviderHealth extends ServiceMap.Service<ProviderHealth, ProviderHealthShape>()(
|
||||
|
||||
@@ -268,7 +268,8 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
),
|
||||
);
|
||||
|
||||
const providerStatuses = yield* providerHealth.getStatuses;
|
||||
// Read provider statuses lazily so background health checks are reflected.
|
||||
const getProviderStatuses = () => Effect.runSync(providerHealth.getStatuses);
|
||||
|
||||
const clients = yield* Ref.make(new Set<WebSocket>());
|
||||
const logger = createLogger("ws");
|
||||
@@ -631,11 +632,23 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: {
|
||||
issues: event.issues,
|
||||
providers: providerStatuses,
|
||||
providers: getProviderStatuses(),
|
||||
},
|
||||
}),
|
||||
).pipe(Effect.forkIn(subscriptionsScope));
|
||||
|
||||
// Push updated provider statuses to connected clients once background health checks finish.
|
||||
providerHealth.onReady((statuses) => {
|
||||
broadcastPush({
|
||||
type: "push",
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: {
|
||||
issues: [],
|
||||
providers: statuses,
|
||||
},
|
||||
}).pipe(Effect.runPromise).catch(() => {});
|
||||
});
|
||||
|
||||
yield* Scope.provide(orchestrationReactor.start, subscriptionsScope);
|
||||
|
||||
let welcomeBootstrapProjectId: ProjectId | undefined;
|
||||
@@ -883,7 +896,7 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
keybindingsConfigPath,
|
||||
keybindings: keybindingsConfig.keybindings,
|
||||
issues: keybindingsConfig.issues,
|
||||
providers: providerStatuses,
|
||||
providers: getProviderStatuses(),
|
||||
availableEditors,
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user