Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/helpers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ const sequencerDB = mysql.createPool(sequencerConfig);

bluebird.promisifyAll([Pool, Connection]);

export const closeDatabase = (): Promise<void> => {
return new Promise(resolve => {
hubDB.end(() => {
sequencerDB.end(() => {
console.log('[mysql] Database connection pools closed');
resolve();
});
});
});
};

export { hubDB as default, sequencerDB };
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import graphql from './graphql';
import { checkKeycard } from './helpers/keycard';
import log from './helpers/log';
import initMetrics from './helpers/metrics';
import { closeDatabase } from './helpers/mysql';
import rateLimit from './helpers/rateLimit';
import refreshSpacesCache from './helpers/spaces';
import './helpers/strategies';
Expand All @@ -32,4 +33,26 @@ app.use('/graphql', graphql);
fallbackLogger(app);
app.get('/*', (req, res) => res.redirect('/api'));

app.listen(PORT, () => log.info(`Started on: http://localhost:${PORT}`));
const server = app.listen(PORT, () =>
log.info(`Started on: http://localhost:${PORT}`)
);

const gracefulShutdown = async (signal: string) => {
console.log(`Received ${signal}. Starting graceful shutdown...`);

server.close(async () => {
console.log('Express server closed.');

try {
await closeDatabase();
console.log('Graceful shutdown completed.');
process.exit(0);
} catch (error) {
console.error('Error during shutdown:', error);
process.exit(1);
}
});
};

process.on('SIGINT', () => gracefulShutdown('SIGINT'));
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));