-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (38 loc) · 1.22 KB
/
Copy pathserver.js
File metadata and controls
43 lines (38 loc) · 1.22 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
const server = require('./src/httpServer');
(async () => {
const serverInstance = server()
try {
await serverInstance.start()
console.log('Server started successfully')
} catch (error) {
console.error('Failed to start server:', error)
process.exit(1)
}
// Handle graceful shutdown
const shutdown = async (signal) => {
console.log(`Received ${signal}, shutting down gracefully...`)
if (!serverInstance) {
console.log('No server instance running, exiting')
process.exit(0)
return
}
// Set a hard timeout to force exit if graceful shutdown takes too long
const forceExitTimeout = setTimeout(() => {
console.error('Shutdown timed out after 10 seconds, forcing exit')
process.exit(1)
}, 10000)
try {
await serverInstance.stop()
clearTimeout(forceExitTimeout)
console.log('Server stopped successfully')
// Small delay to ensure logs are flushed
setTimeout(() => process.exit(0), 100)
} catch (error) {
clearTimeout(forceExitTimeout)
console.error('Error during shutdown:', error)
process.exit(1)
}
}
process.on('SIGTERM', () => shutdown('SIGTERM'))
process.on('SIGINT', () => shutdown('SIGINT'))
})()