File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import 'dotenv/config' ;
22import http from 'http' ;
33import app from './src/app.js' ;
4- import connectDB from './src/config/db.js' ;
5-
4+ import { connectDB , closeDB } from './src/config/db.js' ;
65const PORT = process . env . PORT || 5000 ;
76const NODE_ENV = process . env . NODE_ENV || 'development' ;
87
@@ -34,10 +33,17 @@ const startServer = async() => {
3433
3534startServer ( ) ;
3635
36+ let isShuttingDown = false ;
37+
3738const shutdownGracefully = ( exitCode = 0 ) => {
39+
40+ if ( isShuttingDown ) return ;
41+ isShuttingDown = true ;
42+
3843 if ( server ) {
39- server . close ( ( ) => {
40- console . log ( "Server closed gracefully" ) ;
44+ server . close ( async ( ) => {
45+ await closeDB ( ) ;
46+ console . log ( "Server and DB closed gracefully" ) ;
4147 process . exit ( exitCode ) ;
4248 } ) ;
4349
Original file line number Diff line number Diff line change @@ -10,4 +10,10 @@ const connectDB = async () => {
1010 }
1111} ;
1212
13- export default connectDB ;
13+ const closeDB = async ( ) => {
14+ await mongoose . connection . close ( false ) ;
15+ }
16+ export {
17+ connectDB ,
18+ closeDB
19+ } ;
You can’t perform that action at this time.
0 commit comments