Skip to content

Commit

Permalink
feat: add rabbitmq as another way of receiving alive signals
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmogVC committed Dec 21, 2019
1 parent d239a92 commit 93f2978
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 4 deletions.
122 changes: 118 additions & 4 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"author": "Almog Vagman Ciprut",
"license": "ISC",
"devDependencies": {
"@types/amqplib": "^0.5.13",
"@types/body-parser": "^1.17.1",
"@types/express": "^4.17.2",
"@types/mongoose": "^5.5.34",
Expand All @@ -31,6 +32,7 @@
"typescript": "^3.7.3"
},
"dependencies": {
"amqplib": "^0.5.5",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.8.1"
Expand Down
20 changes: 20 additions & 0 deletions src/aliveSignal/aliveSignal.subscribe.broker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as rabbit from '../utils/rabbit';
import config from '../config';
import AliveSignalManager from './aliveSignal.manager';
import IAliveSignal from './aliveSignal.interface';

export default class AliveSignalSubscribeBroker {
public static async subscribe() {
rabbit.subscribe(
config.rabbitMQ.exchange,
config.rabbitMQ.exchangeType,
config.rabbitMQ.queueName,
'#.aliveSignal',
async messageContent => {
const { hostname, serviceName, aliveDate, upTimeInSeconds } = messageContent;

await AliveSignalManager.create({ hostname, serviceName, aliveDate, upTimeInSeconds });
},
);
}
}
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const config = {
cors: {
allowedOrigins: process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS.split(',') : ['*'],
},
rabbitMQ: {
host: process.env.RMQ_HOST || 'localhost',
port: +(process.env.RMQ_PORT || 5672),
password: process.env.RMQ_PASSWORD || 'guest',
username: process.env.RMQ_USERNAME || 'guest',
exchange: process.env.RMQ_EXCHANGE || 'drop-box-exchange',
exchangeType: process.env.RMQ_EXCHANGE_TYPE || 'topic',
queueName: process.env.RMQ_QUEUE_NAME || 'alive-signal-queue',
},
aliveSignal: {
expirationTimeInSeconds: +(process.env.ALIVE_SIGNAL_EXPIRATION_TIME || 60 * 60 * 24),
},
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as mongoose from 'mongoose';
import * as rabbit from './utils/rabbit';
import Server from './server';
import config from './config';
import AliveSignalSubscribeBroker from './aliveSignal/aliveSignal.subscribe.broker';

initEventHandlers();

Expand All @@ -12,11 +14,13 @@ initEventHandlers();
function initEventHandlers() {
process.on('uncaughtException', error => {
console.error('Unhandled Exception', error.stack);
rabbit.closeConnection();
process.exit(1);
});

process.on('unhandledRejection', error => {
console.error('Unhandled Rejection', error);
rabbit.closeConnection();
process.exit(1);
});

Expand All @@ -25,6 +29,7 @@ function initEventHandlers() {
console.log('User Termination');

await mongoose.disconnect();
rabbit.closeConnection();
process.exit(0);
} catch (error) {
console.error('Failed to close MongoDB connection before server shutdown', error);
Expand Down Expand Up @@ -60,5 +65,8 @@ async function setMongoConnection() {
console.log('[MongoDB] reconnected');
});

await rabbit.connect();
await AliveSignalSubscribeBroker.subscribe();

return mongoose.connect(config.db.connectionString, { useNewUrlParser: true });
}
Loading

0 comments on commit 93f2978

Please sign in to comment.