Repository navigation
Refactor core to DB-injected services with runtime bootstrap #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b0acafe
864cba4
24bb512
2e46f50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import http from "node:http" | ||
|
|
||
| import { resolveAudioDir, startTtsWorker } from "@stash/core" | ||
| import type { FastifyInstance } from "fastify" | ||
| import { createCoreRuntime, resolveAudioDir, type TtsWorkerHandle } from "@stash/core" | ||
| import { createApiApp } from "./create-api-app.js" | ||
| import { serveStatic } from "./static.js" | ||
|
|
||
|
|
@@ -102,7 +103,7 @@ async function listenServer( | |
| } | ||
|
|
||
| async function listenApiServer( | ||
| server: ReturnType<typeof createApiApp>, | ||
| server: FastifyInstance, | ||
| host: string, | ||
| port: number, | ||
| ): Promise<number> { | ||
|
|
@@ -202,32 +203,58 @@ function sendJson(res: http.ServerResponse, statusCode: number, value: unknown): | |
| res.end(body) | ||
| } | ||
|
|
||
| async function shutdownApiResources( | ||
| server: FastifyInstance, | ||
| ttsWorker: TtsWorkerHandle, | ||
| closeRuntime: () => void, | ||
| ): Promise<void> { | ||
| const results = await Promise.allSettled([ttsWorker.stop(), server.close()]) | ||
| closeRuntime() | ||
|
|
||
| const errors = results | ||
| .filter((result): result is PromiseRejectedResult => result.status === "rejected") | ||
| .map((result) => result.reason) | ||
|
|
||
| if (errors.length === 1) { | ||
| throw errors[0] | ||
| } | ||
| if (errors.length > 1) { | ||
| throw new AggregateError(errors, "Failed to shutdown API server resources.") | ||
| } | ||
| } | ||
|
|
||
| export async function startApiServer(options: StartApiServerOptions): Promise<StartedApiServer> { | ||
| const audioDir = resolveAudioDir(options.audioDir) | ||
| const workerOptions = { audioDir } as { audioDir: string; pollMs?: number } | ||
| if (options.ttsWorkerPollMs !== undefined) { | ||
| workerOptions.pollMs = options.ttsWorkerPollMs | ||
| } | ||
|
|
||
| const ttsWorker = startTtsWorker( | ||
| { | ||
| dbPath: options.dbPath, | ||
| migrationsDir: options.migrationsDir, | ||
| }, | ||
| workerOptions, | ||
| ) | ||
| const server = createApiApp({ | ||
| const runtime = createCoreRuntime({ | ||
| dbPath: options.dbPath, | ||
| migrationsDir: options.migrationsDir, | ||
| audioDir, | ||
| }) | ||
| let server: FastifyInstance | ||
| try { | ||
| server = createApiApp({ | ||
| services: runtime.services, | ||
| audioDir, | ||
| }) | ||
| } catch (error) { | ||
| runtime.close() | ||
| throw error | ||
| } | ||
| const ttsWorker = runtime.services.ttsJobs.startTtsWorker(workerOptions) | ||
|
|
||
| let resolvedPort = options.port | ||
| try { | ||
| resolvedPort = await listenApiServer(server, options.host, options.port) | ||
| } catch (error) { | ||
| await server.close() | ||
| await ttsWorker.stop() | ||
| try { | ||
| await shutdownApiResources(server, ttsWorker, runtime.close) | ||
| } catch { | ||
| // Preserve the original listen failure while still attempting full cleanup. | ||
| } | ||
| throw error | ||
| } | ||
|
|
||
|
|
@@ -237,8 +264,7 @@ export async function startApiServer(options: StartApiServerOptions): Promise<St | |
| port: resolvedPort, | ||
| audioDir, | ||
| close: async () => { | ||
| await ttsWorker.stop() | ||
| await server.close() | ||
| await shutdownApiResources(server, ttsWorker, runtime.close) | ||
| }, | ||
|
Comment on lines
266
to
268
|
||
| } | ||
| } | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the failure path for
listenApiServer, the cleanup sequence (await server.close(); await ttsWorker.stop(); runtime.close()) can short-circuit ifserver.close()throws, leaving the worker/runtime running and the SQLite connection open. Consider using nestedtry/finally(orPromise.allSettled) sottsWorker.stop()andruntime.close()are always attempted regardless of which close step fails.