import superagent from 'superagent' ; const PING_ALIVE_DELAY = 1000 * 15 ; const PING_DOWN_DELAY = 1000 * 5 ; /**
 * checks if a service is alive every x seconds
 */ export class PingService { param thisInstance private static async onPingCheck ( thisInstance ) { try { const res = await superagent . get ( thisInstance . host + '/ping' ) . accept ( 'json' ) ; thisInstance . updateDelay ( res . status === 200 ) ; } catch ( error ) { thisInstance . updateDelay ( false ) ; } } private host : string ; private alive : boolean ; private timerId : NodeJS . Timeout ; constructor ( host ) { this . host = host ; } /**
     * performs a ping to the host
     */ public async checkNow ( ) { await PingService . onPingCheck ( this ) ; } /**
     * get the host being checked
     */ public getHost ( ) { return this . host ; } returns public isAlive ( ) { return this . alive ; } /**
     * stops the ping check
     */ public stopTimer ( ) { clearInterval ( this . timerId ) ; } param isAlive private updateDelay ( isAlive ) { if ( this . alive === isAlive ) { return ; } console . warn ( 'Host ' + this . host + ' is now ' + ( isAlive ? 'up' : 'down' ) ) ; clearInterval ( this . timerId ) ; this . timerId = setInterval ( ( ) => { void PingService . onPingCheck ( this ) ; } , isAlive ? PING_ALIVE_DELAY : PING_DOWN_DELAY ) ; this . alive = isAlive ; } }