@@ -17,6 +17,24 @@ const models = require('./models');
1717const Kafka = require ( 'no-kafka' ) ;
1818const 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