fix: actually shut down server on ctrl+c

This commit is contained in:
Kirill Dubovitskiy 2025-07-18 23:57:09 -07:00
parent 7dcb7f30af
commit 40b98cdc17
2 changed files with 23 additions and 10 deletions

View File

@ -1,4 +1,4 @@
import fastify from "fastify"; import fastify, { FastifyInstance } from "fastify";
import { log } from "@/utils/log"; import { log } from "@/utils/log";
import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "fastify-type-provider-zod"; import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "fastify-type-provider-zod";
import { Server, Socket } from "socket.io"; import { Server, Socket } from "socket.io";
@ -34,7 +34,7 @@ declare module 'fastify' {
} }
export async function startApi() { export async function startApi(): Promise<{ app: FastifyInstance; io: Server }> {
// Configure // Configure
log('Starting API...'); log('Starting API...');
@ -120,11 +120,13 @@ export async function startApi() {
} }
// Send to all session-scoped connections, only that match sessionId // Send to all session-scoped connections, only that match sessionId
if (connection.connectionType === 'session-scoped' if (connection.connectionType === 'session-scoped') {
&& connection.sessionId === sessionId const matches = connection.sessionId === sessionId;
) { log({ module: 'websocket' }, `Session-scoped connection ${connection.socket.id}: sessionId=${connection.sessionId}, messageSessionId=${sessionId}, matches=${matches}`);
log({ module: 'websocket' }, `Sending ${event} to session-scoped connection ${connection.socket.id}`); if (matches) {
connection.socket.emit(event, payload); log({ module: 'websocket' }, `Sending ${event} to session-scoped connection ${connection.socket.id}`);
connection.socket.emit(event, payload);
}
} }
} }
} }
@ -615,7 +617,7 @@ export async function startApi() {
} }
const userId = verified.user as string; const userId = verified.user as string;
log({ module: 'websocket' }, `Token verified: ${userId}, clientType: ${clientType || 'user-scoped'}, sessionId: ${sessionId || 'none'}`); log({ module: 'websocket' }, `Token verified: ${userId}, clientType: ${clientType || 'user-scoped'}, sessionId: ${sessionId || 'none'}, socketId: ${socket.id}`);
// Store connection based on type // Store connection based on type
const metadata = { clientType: clientType || 'user-scoped', sessionId }; const metadata = { clientType: clientType || 'user-scoped', sessionId };
@ -761,7 +763,7 @@ export async function startApi() {
socket.on('message', async (data: any) => { socket.on('message', async (data: any) => {
const { sid, message, localId } = data; const { sid, message, localId } = data;
log({ module: 'websocket' }, `Received message from socket ${socket.id}: ${sid} ${message.length} bytes`); log({ module: 'websocket' }, `Received message from socket ${socket.id}: sessionId=${sid}, messageLength=${message.length} bytes, connectionType=${connection.connectionType}, connectionSessionId=${connection.connectionType === 'session-scoped' ? connection.sessionId : 'N/A'}`);
// Resolve session // Resolve session
const session = await db.session.findUnique({ const session = await db.session.findUnique({
@ -1227,4 +1229,6 @@ export async function startApi() {
// End // End
log('API ready on port http://localhost:' + port); log('API ready on port http://localhost:' + port);
return { app, io };
} }

View File

@ -11,7 +11,7 @@ async function main() {
// //
await db.$connect(); await db.$connect();
await startApi(); const { app, io } = await startApi();
startTimeout(); startTimeout();
// //
@ -21,6 +21,15 @@ async function main() {
log('Ready'); log('Ready');
await awaitShutdown(); await awaitShutdown();
log('Shutting down...'); log('Shutting down...');
// Close Socket.io connections
io.close(() => {
log('Socket.io closed');
});
// Close Fastify server
await app.close();
log('Fastify server closed');
} }
main().catch(async (e) => { main().catch(async (e) => {