fix(server): register onClose hook before app.listen
Fastify forbids addHook after the instance is listening, so the sweep-timer cleanup hook from 1.5.0 threw on every `serve` startup and crashed the daemon. Register the hook first, then start listening, and assign the timer through a ref.
This commit is contained in:
@@ -156,10 +156,11 @@ export async function startServer(
|
|||||||
): Promise<{ app: FastifyInstance; store: MailboxStore; sweepTimer: NodeJS.Timeout | null }> {
|
): Promise<{ app: FastifyInstance; store: MailboxStore; sweepTimer: NodeJS.Timeout | null }> {
|
||||||
const store = new MailboxStore(cfg.dbPath);
|
const store = new MailboxStore(cfg.dbPath);
|
||||||
const app = await buildServer(cfg, store);
|
const app = await buildServer(cfg, store);
|
||||||
await app.listen({ host: cfg.bind, port: cfg.port });
|
const timerRef: { current: NodeJS.Timeout | null } = { current: null };
|
||||||
const sweepTimer = startSweep(store, cfg, app.log);
|
|
||||||
app.addHook("onClose", async () => {
|
app.addHook("onClose", async () => {
|
||||||
if (sweepTimer) clearInterval(sweepTimer);
|
if (timerRef.current) clearInterval(timerRef.current);
|
||||||
});
|
});
|
||||||
return { app, store, sweepTimer };
|
await app.listen({ host: cfg.bind, port: cfg.port });
|
||||||
|
timerRef.current = startSweep(store, cfg, app.log);
|
||||||
|
return { app, store, sweepTimer: timerRef.current };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user