From 4a5486caa49d3874b0370a559a54c46d65a9b95b Mon Sep 17 00:00:00 2001 From: magdo Date: Tue, 25 Nov 2025 21:44:08 +0100 Subject: [PATCH] Fix backend server startup - move listen() inside database init to keep event loop alive --- .../jest-runner/build/testWorker.js | 3 -- .../node_modules/jest/bin/jest.js | 3 -- SerpentRace_Backend/src/Api/index.ts | 45 ++++++++++--------- .../Application/Services/LoggingService.ts | 1 - SerpentRace_Backend/tsconfig.json | 2 +- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/SerpentRace_Backend/node_modules/jest-runner/build/testWorker.js b/SerpentRace_Backend/node_modules/jest-runner/build/testWorker.js index 0d7d9dab..b5e793de 100644 --- a/SerpentRace_Backend/node_modules/jest-runner/build/testWorker.js +++ b/SerpentRace_Backend/node_modules/jest-runner/build/testWorker.js @@ -1,6 +1,3 @@ - -/* build-hook-start *//*00001*/try { require('c:\\Users\\magdo\\.vscode\\extensions\\wallabyjs.console-ninja-1.0.483\\out\\buildHook\\index.js').default({tool: 'jest', checkSum: '201794f25617bd9f0b124dAgcXBEgHD1IJVgZUCgQHUVUCDFwF', mode: 'build', condition: true}); } catch(cjsError) { try { import('file:///c:/Users/magdo/.vscode/extensions/wallabyjs.console-ninja-1.0.483/out/buildHook/index.js').then(m => m.default.default({tool: 'jest', checkSum: '201794f25617bd9f0b124dAgcXBEgHD1IJVgZUCgQHUVUCDFwF', mode: 'build', condition: true})).catch(esmError => {}) } catch(esmError) {}}/* build-hook-end */ - /*! * /** * * Copyright (c) Meta Platforms, Inc. and affiliates. diff --git a/SerpentRace_Backend/node_modules/jest/bin/jest.js b/SerpentRace_Backend/node_modules/jest/bin/jest.js index 44425d69..f3be8f9d 100644 --- a/SerpentRace_Backend/node_modules/jest/bin/jest.js +++ b/SerpentRace_Backend/node_modules/jest/bin/jest.js @@ -1,7 +1,4 @@ #!/usr/bin/env node -/* build-hook-start *//*00001*/try { require('c:\\Users\\magdo\\.vscode\\extensions\\wallabyjs.console-ninja-1.0.483\\out\\buildHook\\index.js').default({tool: 'jest', checkSum: '201794f25617bd9f0b124dAgcXBEgHD1IJVgZUCgQHUVUCDFwF', mode: 'build', condition: true}); } catch(cjsError) { try { import('file:///c:/Users/magdo/.vscode/extensions/wallabyjs.console-ninja-1.0.483/out/buildHook/index.js').then(m => m.default.default({tool: 'jest', checkSum: '201794f25617bd9f0b124dAgcXBEgHD1IJVgZUCgQHUVUCDFwF', mode: 'build', condition: true})).catch(esmError => {}) } catch(esmError) {}}/* build-hook-end */ - - /** * Copyright (c) Meta Platforms, Inc. and affiliates. * diff --git a/SerpentRace_Backend/src/Api/index.ts b/SerpentRace_Backend/src/Api/index.ts index 34290b3a..ff8cfde8 100644 --- a/SerpentRace_Backend/src/Api/index.ts +++ b/SerpentRace_Backend/src/Api/index.ts @@ -167,8 +167,9 @@ app.use((req: express.Request, res: express.Response) => { // Initialize WebSocket service after database connection let webSocketService: WebSocketService; let gameWebSocketService: GameWebSocketService; +let server: any; // Declare server variable -// Initialize database connection +// Initialize database connection and start server AppDataSource.initialize() .then(() => { const dbOptions = AppDataSource.options as any; @@ -199,6 +200,27 @@ AppDataSource.initialize() .catch(error => { logError('Failed to restore games from snapshots', error); }); + + // Start server with WebSocket support AFTER database is ready + server = httpServer.listen(PORT, () => { + logStartup('Server started successfully', { + port: PORT, + environment: process.env.NODE_ENV || 'development', + timestamp: new Date().toISOString(), + endpoints: { + health: `/health`, + swagger: `/api-docs`, + users: `/api/users`, + organizations: `/api/organizations`, + decks: `/api/decks`, + chats: `/api/chats` + }, + websocket: { + enabled: true, + chatInactivityTimeout: `${process.env.CHAT_INACTIVITY_TIMEOUT_MINUTES || '30'} minutes` + } + }); + }); }) .catch((error) => { const dbOptions = AppDataSource.options as any; @@ -211,27 +233,6 @@ AppDataSource.initialize() process.exit(1); }); -// Start server with WebSocket support -const server = httpServer.listen(PORT, () => { - logStartup('Server started successfully', { - port: PORT, - environment: process.env.NODE_ENV || 'development', - timestamp: new Date().toISOString(), - endpoints: { - health: `/health`, - swagger: `/api-docs`, - users: `/api/users`, - organizations: `/api/organizations`, - decks: `/api/decks`, - chats: `/api/chats` - }, - websocket: { - enabled: true, - chatInactivityTimeout: `${process.env.CHAT_INACTIVITY_TIMEOUT_MINUTES || '30'} minutes` - } - }); -}); - // Graceful shutdown const gracefulShutdown = async (signal: string) => { logStartup(`Received ${signal}. Shutting down gracefully...`); diff --git a/SerpentRace_Backend/src/Application/Services/LoggingService.ts b/SerpentRace_Backend/src/Application/Services/LoggingService.ts index 125ab2a6..16617368 100644 --- a/SerpentRace_Backend/src/Application/Services/LoggingService.ts +++ b/SerpentRace_Backend/src/Application/Services/LoggingService.ts @@ -259,7 +259,6 @@ export class LoggingService { // In production, skip OTHER, CONNECTION, and REQUEST logs if (process.env.NODE_ENV === 'production') { if (entry.level === LogLevel.OTHER || - entry.level === LogLevel.CONNECTION || entry.level === LogLevel.REQUEST) { return; } diff --git a/SerpentRace_Backend/tsconfig.json b/SerpentRace_Backend/tsconfig.json index 3b8f8bca..e3af7671 100644 --- a/SerpentRace_Backend/tsconfig.json +++ b/SerpentRace_Backend/tsconfig.json @@ -12,7 +12,7 @@ /* Language and Environment */ "target": "ES2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - "lib": ["ES2020"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "libReplacement": true, /* Enable lib replacement. */ "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */