@@ -17,6 +17,24 @@ const models = require('./models');
17
17
const Kafka = require ( 'no-kafka' ) ;
18
18
const healthcheck = require ( 'topcoder-healthcheck-dropin' ) ;
19
19
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
+
20
38
/**
21
39
* Start Kafka consumer for event bus events.
22
40
* @param {Object } handlers the handlers
@@ -79,8 +97,12 @@ function startKafkaConsumer(handlers, notificationServiceHandlers) {
79
97
80
98
const check = function ( ) {
81
99
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
+ }
82
104
if ( ! consumer . client . initialBrokers && ! consumer . client . initialBrokers . length ) {
83
- logger . debug ( 'Found unhealthy Kafka Brokers...' ) ;
105
+ logger . error ( 'Found unhealthy Kafka Brokers...' ) ;
84
106
return false ;
85
107
}
86
108
let connected = true ;
0 commit comments