@@ -8,6 +8,7 @@ import graphql from './graphql';
8
8
import { checkKeycard } from './helpers/keycard' ;
9
9
import log from './helpers/log' ;
10
10
import initMetrics from './helpers/metrics' ;
11
+ import { closeDatabase } from './helpers/mysql' ;
11
12
import rateLimit from './helpers/rateLimit' ;
12
13
import refreshSpacesCache from './helpers/spaces' ;
13
14
import './helpers/strategies' ;
@@ -32,4 +33,26 @@ app.use('/graphql', graphql);
32
33
fallbackLogger ( app ) ;
33
34
app . get ( '/*' , ( req , res ) => res . redirect ( '/api' ) ) ;
34
35
35
- app . listen ( PORT , ( ) => log . info ( `Started on: http://localhost:${ PORT } ` ) ) ;
36
+ const server = app . listen ( PORT , ( ) =>
37
+ log . info ( `Started on: http://localhost:${ PORT } ` )
38
+ ) ;
39
+
40
+ const gracefulShutdown = async ( signal : string ) => {
41
+ log . info ( `Received ${ signal } . Starting graceful shutdown...` ) ;
42
+
43
+ server . close ( async ( ) => {
44
+ log . info ( 'Express server closed.' ) ;
45
+
46
+ try {
47
+ await closeDatabase ( ) ;
48
+ log . info ( 'Graceful shutdown completed.' ) ;
49
+ process . exit ( 0 ) ;
50
+ } catch ( error ) {
51
+ log . error ( 'Error during shutdown:' , error ) ;
52
+ process . exit ( 1 ) ;
53
+ }
54
+ } ) ;
55
+ } ;
56
+
57
+ process . on ( 'SIGINT' , ( ) => gracefulShutdown ( 'SIGINT' ) ) ;
58
+ process . on ( 'SIGTERM' , ( ) => gracefulShutdown ( 'SIGTERM' ) ) ;
0 commit comments