Skip to content

Commit

Permalink
Refactor app shutdown process to use async/await for disconnecting se…
Browse files Browse the repository at this point in the history
…rvices and improve logging level in logger configuration
  • Loading branch information
austenstone committed Nov 27, 2024
1 parent 46254e8 commit ce62ded
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
37 changes: 18 additions & 19 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ class App {
}
}

public stop() {
whyIsNodeRunning()
this.database.disconnect();
this.github.disconnect();
this.eListener?.close(() => {
logger.info('Server closed');
process.exit(0);
});
public async stop() {
await new Promise(resolve => this.eListener?.close(resolve));
await this.database.disconnect();
await this.github.disconnect();
}

private setupExpress() {
Expand All @@ -78,15 +74,17 @@ class App {
const __dirname = dirname(__filename);
const frontendPath = path.resolve(__dirname, '../../frontend/dist/github-value/browser');
this.e.use(express.static(frontendPath));
this.e.get('*', rateLimit({
windowMs: 15 * 60 * 1000, max: 5000,
}), (_, res) => res.sendFile(path.join(frontendPath, 'index.html')));
this.e.get(
'*',
rateLimit({
windowMs: 15 * 60 * 1000, max: 5000,
}),
(_, res) => res.sendFile(path.join(frontendPath, 'index.html'))
);

const listener = this.e.listen(this.port, () => {
const address = listener.address() as AddressInfo;
logger.info(`Server is running at http://${address.address === '::' ? 'localhost' : address.address}:${address.port} 🚀`);
});
this.eListener = listener;
this.eListener = this.e.listen(this.port, () => {
logger.info(`Server is running 🚀`);
});;
}

private initializeSettings() {
Expand Down Expand Up @@ -162,9 +160,10 @@ logger.info('App started');
export default app;

['SIGTERM', 'SIGINT', 'SIGQUIT'].forEach(signal => {
process.on(signal, () => {
process.on(signal, async () => {
logger.info(`Received ${signal}. Stopping the app...`);
app.stop();
process.exit(signal === 'uncaughtException' ? 1 : 0);
await app.stop();
whyIsNodeRunning()
process.exit(0);
});
});
6 changes: 3 additions & 3 deletions backend/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class Database {
}
}

disconnect() {
this.sequelize?.connectionManager.close();
this.sequelize?.close();
async disconnect() {
await this.sequelize?.connectionManager.close();
await this.sequelize?.close();
}

initializeModels(sequelize: Sequelize) {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const logger = bunyan.createLogger({
},
streams: [
{
level: 'info',
level: 'debug',
stream: process.stdout
},
{
Expand Down

0 comments on commit ce62ded

Please sign in to comment.