Skip to content

Commit

Permalink
refactor: refactor shutdown connection typeorm and rabbitmq
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Dec 19, 2023
1 parent 9fbb310 commit c675e1e
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 39 deletions.
10 changes: 3 additions & 7 deletions src/booking/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const startupApp = async () => {

app.use(morganMiddleware);

const databaseConnection = await initialDbContext(postgresOptions);
await initialDbContext(postgresOptions);

app.use(helmet());

Expand All @@ -60,7 +60,7 @@ const startupApp = async () => {
logger.info(`Listening to http://localhost:${config.port}`);
});

const rabbitmq = await initialRabbitmq();
await initialRabbitmq();

await initialHttpClientServices();

Expand All @@ -71,11 +71,7 @@ const startupApp = async () => {
}

process.on('SIGINT', async () => {
await databaseConnection.destroy();
await rabbitmq.closeConnection();
server.close(function () {
process.exit(0);
});
server.close();
Logger.info('Application shutdown gracefully.');
});
};
Expand Down
11 changes: 8 additions & 3 deletions src/building-blocks/rabbitmq/rabbitmq-connection.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/building-blocks/rabbitmq/rabbitmq-connection.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/building-blocks/rabbitmq/rabbitmq-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export class RabbitMQConnection implements IRabbitMQConnection {
password: options?.password ?? config.rabbitmq.password
});

Logger.info('RabbitMq connection created successfully');
Logger.info('Rabbitmq connection created successfully');

process.on('SIGINT', async () => {
if (connection) {
await this.closeConnection();
}
});
},
{
retries: config.retry.count,
Expand Down Expand Up @@ -112,10 +118,10 @@ export class RabbitMQConnection implements IRabbitMQConnection {
try {
if (connection) {
await connection.close();
Logger.info('Connection closed successfully');
Logger.info('Connection rabbitmq closed gracefully!');
}
} catch (error) {
Logger.error('Connection close failed!');
Logger.error('Connection rabbitmq close failed!');
}
}
}
2 changes: 1 addition & 1 deletion src/building-blocks/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/building-blocks/typeorm/db-context.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/building-blocks/typeorm/db-context.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/building-blocks/typeorm/db-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export class DbContext implements IDbContext {

Logger.info('Data Source has been initialized!');

process.on('SIGINT', async () => {
if (connection) {
await this.closeConnection();
}
});

if (config.env !== 'test') {
try {
} catch (error) {
Expand All @@ -34,7 +40,7 @@ export class DbContext implements IDbContext {
Logger.info('Migrations run successfully!');
}
} catch (error) {
throw new Error(`Error during database initialization: ${error.toString()}`);
throw new Error(error);
}

return connection;
Expand All @@ -46,5 +52,6 @@ export class DbContext implements IDbContext {

async closeConnection(): Promise<void> {
await connection.destroy();
Logger.info('Connection postgres destroyed gracefully!');
}
}
10 changes: 3 additions & 7 deletions src/flight/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const startupApp = async () => {

app.use(morganMiddleware);

const databaseConnection = await initialDbContext(postgresOptions);
await initialDbContext(postgresOptions);

app.use(helmet());

Expand All @@ -59,7 +59,7 @@ const startupApp = async () => {
logger.info(`Listening http://localhost:${config.port}`);
});

const rabbitmq = await initialRabbitmq();
await initialRabbitmq();

await registerMediatrHandlers();

Expand All @@ -68,11 +68,7 @@ const startupApp = async () => {
}

process.on('SIGINT', async () => {
await databaseConnection.destroy();
await rabbitmq.closeConnection();
server.close(function () {
process.exit(0);
});
server.close();
Logger.info('Application shutdown gracefully.');
});
};
Expand Down
10 changes: 3 additions & 7 deletions src/identity/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const startupApp = async () => {

app.use(morganMiddleware);

const databaseConnection = await initialDbContext(postgresOptions);
await initialDbContext(postgresOptions);

app.use(helmet());

Expand All @@ -59,7 +59,7 @@ const startupApp = async () => {
logger.info(`Listening to http://localhost:${config.port}`);
});

const rabbitmq = await initialRabbitmq();
await initialRabbitmq();

await registerMediatrHandlers();

Expand All @@ -68,11 +68,7 @@ const startupApp = async () => {
}

process.on('SIGINT', async () => {
await databaseConnection.destroy();
await rabbitmq.closeConnection();
server.close(function () {
process.exit(0);
});
server.close();
Logger.info('Application shutdown gracefully.');
});
};
Expand Down
10 changes: 3 additions & 7 deletions src/passenger/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const startupApp = async () => {

app.use(morganMiddleware);

const databaseConnection = await initialDbContext(postgresOptions);
await initialDbContext(postgresOptions);

app.use(helmet());

Expand All @@ -59,7 +59,7 @@ const startupApp = async () => {
logger.info(`Listening to http://localhost:${config.port}`);
});

const rabbitmq = await initialRabbitmq();
await initialRabbitmq();

await registerMediatrHandlers();

Expand All @@ -68,11 +68,7 @@ const startupApp = async () => {
}

process.on('SIGINT', async () => {
await databaseConnection.destroy();
await rabbitmq.closeConnection();
server.close(function () {
process.exit(0);
});
server.close();
Logger.info('Application shutdown gracefully.');
});
};
Expand Down

0 comments on commit c675e1e

Please sign in to comment.