overleaf/services/real-time/app/js/HealthCheckManager.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

77 lines
2.1 KiB
JavaScript

const metrics = require('@overleaf/metrics')
const logger = require('@overleaf/logger')
const os = require('node:os')
const HOST = os.hostname()
const PID = process.pid
let COUNT = 0
const CHANNEL_MANAGER = {} // hash of event checkers by channel name
const CHANNEL_ERROR = {} // error status by channel name
module.exports = class HealthCheckManager {
// 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(() => {
this.setStatus()
}, timeout || 1000)
// 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
}
processEvent(id) {
// if this is our event record it
if (id === this.id) {
this.count++
if (this.timer) {
this.timer.done()
}
this.timer = undefined // only time the latency of the first event
}
}
setStatus() {
// if we saw the event anything other than a single time that is an error
const isFailing = this.count !== 1
if (isFailing) {
logger.err(
{ channel: this.channel, count: this.count, id: this.id },
'redis channel health check error'
)
}
CHANNEL_ERROR[this.channel] = isFailing
}
// class methods
static check(channel, id) {
// dispatch event to manager for channel
if (CHANNEL_MANAGER[channel]) {
CHANNEL_MANAGER[channel].processEvent(id)
}
}
static status() {
// return status of all channels for logging
return CHANNEL_ERROR
}
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
}
}
return false
}
}