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
+23 -22
View File
@@ -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...`);