import { Logger } from 'winston' ; export class Heartbeat { private logger : Logger ; private lastHeartbeat : Date ; private threshold : number ; private startTime : number = Date . now ( ) ; constructor ( logger , threshold = 300 ) { this . logger = logger ; this . lastHeartbeat = new Date ( ) ; this . threshold = threshold ; setInterval ( this . checkHealth . bind ( this ) , threshold * 500 ) ; } public updateHeartbeat ( ) { this . lastHeartbeat = new Date ( ) ; } public checkHealth ( ) { const current = new Date ( ) ; if ( ( current . getTime ( ) - this . lastHeartbeat . getTime ( ) ) / 1000 > this . threshold ) { this . logger . error ( `Hearbeat timeout, last hearbeat:${ this . lastHeartbeat }, current time:${ current }, gap: ${ Math . floor ( ( current . getTime ( ) - this . lastHeartbeat . getTime ( ) ) / 1000 ) } seconds, duration: ${ Math . floor ( ( current . getTime ( ) - this . startTime ) / 1000 ) } seconds` ) ; process . exit ( 1 ) ; } } }