@@ -2,7 +2,6 @@ import 'dotenv/config';
22import http from 'http' ;
33import app from './src/app.js' ;
44import connectDB from './src/config/db.js' ;
5- import { start } from 'repl' ;
65
76const PORT = process . env . PORT || 5000 ;
87const NODE_ENV = process . env . NODE_ENV || 'development' ;
@@ -35,35 +34,39 @@ const startServer = async() => {
3534
3635startServer ( ) ;
3736
38- process . on ( "unhandledRejection" , ( err ) => {
39- console . error ( "Unhandled Rejection: " , err ) ;
40- shutdownGracefully ( ) ;
41- } ) ;
42-
43- process . on ( "uncaughtException" , ( err ) => {
44- console . error ( "Uncaught Exception: " , err ) ;
45- shutdownGracefully ( ) ;
46- } ) ;
47-
48- const shutdownGracefully = ( ) => {
37+ const shutdownGracefully = ( exitCode = 0 ) => {
4938 if ( server ) {
5039 server . close ( ( ) => {
51- console . log ( "Server closed grecefully " ) ;
52- process . exit ( 1 ) ;
40+ console . log ( "Server closed gracefully " ) ;
41+ process . exit ( exitCode ) ;
5342 } ) ;
43+
44+ setTimeout ( ( ) => {
45+ console . error ( "Forced shutdown after timeout" ) ;
46+ process . exit ( exitCode ) ;
47+ } , 10000 ) . unref ( ) ;
5448 }
5549 else {
56- process . exit ( 1 ) ;
50+ process . exit ( exitCode ) ;
5751 }
5852} ;
5953
54+ process . on ( "unhandledRejection" , ( err ) => {
55+ console . error ( "Unhandled Rejection: " , err ) ;
56+ shutdownGracefully ( 1 ) ;
57+ } ) ;
58+
59+ process . on ( "uncaughtException" , ( err ) => {
60+ console . error ( "Uncaught Exception: " , err ) ;
61+ shutdownGracefully ( 1 ) ;
62+ } ) ;
63+
6064process . on ( "SIGTERM" , ( ) => {
6165 console . log ( "SIGTERM received. Shutting down..." ) ;
6266 shutdownGracefully ( ) ;
6367} ) ;
6468
6569process . on ( "SIGINT" , ( ) => {
66- console . log ( "SIGINT reveiced . Shutting down..." ) ;
70+ console . log ( "SIGINT received . Shutting down..." ) ;
6771 shutdownGracefully ( ) ;
68- } ) ;
69-
72+ } ) ;
0 commit comments