Fix backend server startup - move listen() inside database init to keep event loop alive

This commit is contained in:
magdo
2025-11-25 21:44:08 +01:00
parent 73c939e75b
commit 4a5486caa4
5 changed files with 24 additions and 30 deletions
-3
View File
@@ -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. * * Copyright (c) Meta Platforms, Inc. and affiliates.
-3
View File
@@ -1,7 +1,4 @@
#!/usr/bin/env node #!/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. * Copyright (c) Meta Platforms, Inc. and affiliates.
* *
+15 -14
View File
@@ -167,8 +167,9 @@ app.use((req: express.Request, res: express.Response) => {
// Initialize WebSocket service after database connection // Initialize WebSocket service after database connection
let webSocketService: WebSocketService; let webSocketService: WebSocketService;
let gameWebSocketService: GameWebSocketService; let gameWebSocketService: GameWebSocketService;
let server: any; // Declare server variable
// Initialize database connection // Initialize database connection and start server
AppDataSource.initialize() AppDataSource.initialize()
.then(() => { .then(() => {
const dbOptions = AppDataSource.options as any; const dbOptions = AppDataSource.options as any;
@@ -199,20 +200,9 @@ AppDataSource.initialize()
.catch(error => { .catch(error => {
logError('Failed to restore games from snapshots', error); logError('Failed to restore games from snapshots', error);
}); });
})
.catch((error) => {
const dbOptions = AppDataSource.options as any;
logConnection('Database connection failed', 'postgresql', 'failure', {
error: error.message,
type: dbOptions.type,
host: dbOptions.host,
database: dbOptions.database
});
process.exit(1);
});
// Start server with WebSocket support // Start server with WebSocket support AFTER database is ready
const server = httpServer.listen(PORT, () => { server = httpServer.listen(PORT, () => {
logStartup('Server started successfully', { logStartup('Server started successfully', {
port: PORT, port: PORT,
environment: process.env.NODE_ENV || 'development', environment: process.env.NODE_ENV || 'development',
@@ -231,6 +221,17 @@ const server = httpServer.listen(PORT, () => {
} }
}); });
}); });
})
.catch((error) => {
const dbOptions = AppDataSource.options as any;
logConnection('Database connection failed', 'postgresql', 'failure', {
error: error.message,
type: dbOptions.type,
host: dbOptions.host,
database: dbOptions.database
});
process.exit(1);
});
// Graceful shutdown // Graceful shutdown
const gracefulShutdown = async (signal: string) => { const gracefulShutdown = async (signal: string) => {
@@ -259,7 +259,6 @@ export class LoggingService {
// In production, skip OTHER, CONNECTION, and REQUEST logs // In production, skip OTHER, CONNECTION, and REQUEST logs
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
if (entry.level === LogLevel.OTHER || if (entry.level === LogLevel.OTHER ||
entry.level === LogLevel.CONNECTION ||
entry.level === LogLevel.REQUEST) { entry.level === LogLevel.REQUEST) {
return; return;
} }
+1 -1
View File
@@ -12,7 +12,7 @@
/* Language and Environment */ /* Language and Environment */
"target": "ES2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "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. */ // "jsx": "preserve", /* Specify what JSX code is generated. */
// "libReplacement": true, /* Enable lib replacement. */ // "libReplacement": true, /* Enable lib replacement. */
"experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */