-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (50 loc) · 1.7 KB
/
server.js
File metadata and controls
61 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import app from './src/app.js';
import config from './src/config/config.js';
import logger from './src/loggers/winston.logger.js';
import connectToDatabase from './src/config/db.js';
import redisService from './src/config/redis.js';
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
logger.error(`Uncaught Exception: ${error.message}`, { stack: error.stack });
process.exit(1);
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
logger.error(`Unhandled Rejection at: ${promise}, reason: ${reason}`);
process.exit(1);
});
// Connect to database
connectToDatabase();
// Connect to Redis explicitly - this ensures a single connection
redisService.connect().then((connected) => {
if (connected) {
logger.info('Redis client successfully connected');
} else {
logger.warn('Failed to connect to Redis - check your configuration');
}
});
// Start the server
const server = app.listen(config.PORT, () => {
logger.info(`Server is running on port ${config.PORT}`);
logger.debug(`Environment: ${process.env.NODE_ENV || 'development'}`);
});
// Graceful shutdown
const gracefulShutdown = async () => {
logger.info('Starting graceful shutdown...');
// Close the server
server.close(async () => {
logger.info('HTTP server closed');
// Disconnect from Redis
await redisService.disconnect();
logger.info('Graceful shutdown completed');
process.exit(0);
});
// Force shutdown after timeout
setTimeout(() => {
logger.error('Forcing shutdown after timeout');
process.exit(1);
}, 10000);
};
// Listen for termination signals
process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT', gracefulShutdown);