Skip to content

Commit 28161f7

Browse files
author
Vikas Agarwal
committed
Instead of aborting the process we now let health check to convey the message for more graceful process termination.
1 parent 0d01c60 commit 28161f7

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

connect/connectNotificationServer.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,5 @@ if (config.ENABLE_EMAILS) {
507507
// notificationServer.logger.error('Notification server errored out');
508508
// });
509509

510-
511-
process.on('unhandledRejection', (reason, promise) => {
512-
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
513-
// aborts the process to let the HA of the container to restart the task
514-
process.abort();
515-
});
516-
517510
// if no need to init database, then directly start the server:
518511
notificationServer.startKafkaConsumers();

src/app.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ const models = require('./models');
1717
const Kafka = require('no-kafka');
1818
const healthcheck = require('topcoder-healthcheck-dropin');
1919

20+
21+
// helps in health checking in case of unhandled rejection of promises
22+
const unhandledRejections = [];
23+
process.on('unhandledRejection', (reason, promise) => {
24+
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
25+
// aborts the process to let the HA of the container to restart the task
26+
// process.abort();
27+
unhandledRejections.push(promise);
28+
});
29+
30+
// ideally any unhandled rejection is handled after more than one event loop, it should be removed
31+
// from the unhandledRejections array. We just remove the first element from the array as we only care
32+
// about the count every time an unhandled rejection promise is handled
33+
process.on('rejectionHandled', (promise) => {
34+
console.log('Handled Rejection at:', promise);
35+
unhandledRejections.shift();
36+
});
37+
2038
/**
2139
* Start Kafka consumer for event bus events.
2240
* @param {Object} handlers the handlers
@@ -79,8 +97,12 @@ function startKafkaConsumer(handlers, notificationServiceHandlers) {
7997

8098
const check = function () {
8199
logger.debug("Checking health");
100+
if (unhandledRejections && unhandledRejections.length > 0) {
101+
logger.error('Found unhandled promises. Application is potentially in stalled state.');
102+
return false;
103+
}
82104
if (!consumer.client.initialBrokers && !consumer.client.initialBrokers.length) {
83-
logger.debug('Found unhealthy Kafka Brokers...');
105+
logger.error('Found unhealthy Kafka Brokers...');
84106
return false;
85107
}
86108
let connected = true;

0 commit comments

Comments
 (0)