File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -37,4 +37,15 @@ const sequencerDB = mysql.createPool(sequencerConfig);
37
37
38
38
bluebird . promisifyAll ( [ Pool , Connection ] ) ;
39
39
40
+ export const closeDatabase = ( ) : Promise < void > => {
41
+ return new Promise ( resolve => {
42
+ hubDB . end ( ( ) => {
43
+ sequencerDB . end ( ( ) => {
44
+ console . log ( '[mysql] Database connection pools closed' ) ;
45
+ resolve ( ) ;
46
+ } ) ;
47
+ } ) ;
48
+ } ) ;
49
+ } ;
50
+
40
51
export { hubDB as default , sequencerDB } ;
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import initMetrics from './helpers/metrics';
11
11
import rateLimit from './helpers/rateLimit' ;
12
12
import refreshSpacesCache from './helpers/spaces' ;
13
13
import './helpers/strategies' ;
14
+ import { closeDatabase } from './helpers/mysql' ;
14
15
15
16
const app = express ( ) ;
16
17
const PORT = process . env . PORT || 3000 ;
@@ -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
+ console . log ( `Received ${ signal } . Starting graceful shutdown...` ) ;
42
+
43
+ server . close ( async ( ) => {
44
+ console . log ( 'Express server closed.' ) ;
45
+
46
+ try {
47
+ await closeDatabase ( ) ;
48
+ console . log ( 'Graceful shutdown completed.' ) ;
49
+ process . exit ( 0 ) ;
50
+ } catch ( error ) {
51
+ console . 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' ) ) ;
You can’t perform that action at this time.
0 commit comments