0

I am trying to create kafka consumer health check using event emmitter. I have tried to implement the following code to check heartbeat and consumer.crash event:

 const isHealthy = async function()  {
  const { HEARTBEAT } = consumer.events;
  let lastHeartbeat;
  let crashVal;
  console.log(consumer);
consumer.on(HEARTBEAT, ({timestamp}) => {
    console.log("Inside consumer on HeartbeatVal: "+timestamp);
});
console.log("consumer after binding hearbeat"+JSON.stringify(consumer));
consumer.on('consumer.crash', event => {
    const error = event?.payload?.error;
    crashVal=error;
    console.log("Hello error: "+JSON.stringify(error));
})
console.log("diff"+Date.now() - lastHeartbeat);
  if (Date.now() - lastHeartbeat < SESSION_TIMEOUT) {
    return true;
  }
 // Consumer has not heartbeat, but maybe it's because the group is currently rebalancing
  try {
    console.log("Inside Describe group");
    const flag=await consumer.describeGroup()
    const { state } = await consumer.describeGroup()
    console.log("state: "+state);
    if(state==='Stable'){
        return true;
    }

   return ['CompletingRebalance', 'PreparingRebalance'].includes(state)
  } catch (ex) {
    return false
  }
}

Could you help to provide any solution for the above.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 15 '22 at 16:38
  • were you able to find a fix for this ? – Kaneki21 Aug 02 '23 at 12:17

0 Answers0