2020-11-25 06:57:22 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2020-06-23 13:29:44 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2019-04-15 09:05:26 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const os = require('os')
|
|
|
|
const HOST = os.hostname()
|
|
|
|
const PID = process.pid
|
|
|
|
let COUNT = 0
|
2019-04-15 09:05:26 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const CHANNEL_MANAGER = {} // hash of event checkers by channel name
|
|
|
|
const CHANNEL_ERROR = {} // error status by channel name
|
2019-04-15 09:05:26 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
module.exports = class HealthCheckManager {
|
2020-06-23 13:29:44 -04:00
|
|
|
// create an instance of this class which checks that an event with a unique
|
|
|
|
// id is received only once within a timeout
|
|
|
|
constructor(channel, timeout) {
|
|
|
|
// unique event string
|
|
|
|
this.channel = channel
|
|
|
|
this.id = `host=${HOST}:pid=${PID}:count=${COUNT++}`
|
|
|
|
// count of number of times the event is received
|
|
|
|
this.count = 0
|
|
|
|
// after a timeout check the status of the count
|
|
|
|
this.handler = setTimeout(() => {
|
2020-07-07 06:06:02 -04:00
|
|
|
this.setStatus()
|
|
|
|
}, timeout || 1000)
|
2020-06-23 13:29:44 -04:00
|
|
|
// use a timer to record the latency of the channel
|
|
|
|
this.timer = new metrics.Timer(`event.${this.channel}.latency`)
|
|
|
|
// keep a record of these objects to dispatch on
|
|
|
|
CHANNEL_MANAGER[this.channel] = this
|
|
|
|
}
|
2020-06-23 13:29:38 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
processEvent(id) {
|
|
|
|
// if this is our event record it
|
|
|
|
if (id === this.id) {
|
|
|
|
this.count++
|
2020-07-07 06:06:02 -04:00
|
|
|
if (this.timer) {
|
2020-06-23 13:29:44 -04:00
|
|
|
this.timer.done()
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
this.timer = undefined // only time the latency of the first event
|
2020-06-23 13:29:34 -04:00
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-06-23 13:29:38 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
setStatus() {
|
|
|
|
// if we saw the event anything other than a single time that is an error
|
2020-07-07 06:06:02 -04:00
|
|
|
const isFailing = this.count !== 1
|
|
|
|
if (isFailing) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.err(
|
|
|
|
{ channel: this.channel, count: this.count, id: this.id },
|
|
|
|
'redis channel health check error'
|
|
|
|
)
|
2020-06-23 13:29:34 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
CHANNEL_ERROR[this.channel] = isFailing
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2019-04-15 09:05:26 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// class methods
|
|
|
|
static check(channel, id) {
|
|
|
|
// dispatch event to manager for channel
|
2020-07-07 06:06:02 -04:00
|
|
|
if (CHANNEL_MANAGER[channel]) {
|
|
|
|
CHANNEL_MANAGER[channel].processEvent(id)
|
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-06-23 13:29:38 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
static status() {
|
|
|
|
// return status of all channels for logging
|
|
|
|
return CHANNEL_ERROR
|
|
|
|
}
|
2020-06-23 13:29:38 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
static isFailing() {
|
|
|
|
// check if any channel status is bad
|
|
|
|
for (const channel in CHANNEL_ERROR) {
|
|
|
|
const error = CHANNEL_ERROR[channel]
|
|
|
|
if (error === true) {
|
|
|
|
return true
|
|
|
|
}
|
2020-06-23 13:29:34 -04:00
|
|
|
}
|
2020-06-23 13:29:44 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|