Skip to content

Commit

Permalink
feat(database): Export redis from db.ts and handle graceful shutdown …
Browse files Browse the repository at this point in the history
…with db and redis connections
  • Loading branch information
wajeht committed Aug 7, 2024
1 parent 9c57dd6 commit 3f62b24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/database/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import knex from 'knex';
import knexConfig from './knexfile';

export { redis } from './redis';
export const db = knex(knexConfig);
19 changes: 18 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { app } from './app';
import { Server } from 'http';
import { AddressInfo } from 'net';
import { appConfig } from './config';
import { db, redis } from './database/db';

const server: Server = app.listen(appConfig.port);

Expand Down Expand Up @@ -37,8 +38,24 @@ server.on('error', (error: NodeJS.ErrnoException) => {
function gracefulShutdown(signal: string): void {
console.info(`Received ${signal}, shutting down gracefully.`);

server.close(() => {
server.close(async () => {
console.info('HTTP server closed.');

try {
redis.quit();
console.info('Redis connection closed.');
} catch (error) {
console.error('Error closing Redis connection:', error);
}

try {
await db.destroy();
console.info('Database connection closed.');
} catch (error) {
console.error('Error closing database connection:', error);
}

console.info('All connections closed successfully.');
process.exit(0);
});

Expand Down

0 comments on commit 3f62b24

Please sign in to comment.