HealthCheckManager.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let HealthCheckManager
  14. const metrics = require('metrics-sharelatex')
  15. const logger = require('logger-sharelatex')
  16. const os = require('os')
  17. const HOST = os.hostname()
  18. const PID = process.pid
  19. let COUNT = 0
  20. const CHANNEL_MANAGER = {} // hash of event checkers by channel name
  21. const CHANNEL_ERROR = {} // error status by channel name
  22. module.exports = HealthCheckManager = class HealthCheckManager {
  23. // create an instance of this class which checks that an event with a unique
  24. // id is received only once within a timeout
  25. constructor(channel, timeout) {
  26. // unique event string
  27. this.channel = channel
  28. if (timeout == null) {
  29. timeout = 1000
  30. }
  31. this.id = `host=${HOST}:pid=${PID}:count=${COUNT++}`
  32. // count of number of times the event is received
  33. this.count = 0
  34. // after a timeout check the status of the count
  35. this.handler = setTimeout(() => {
  36. return this.setStatus()
  37. }, timeout)
  38. // use a timer to record the latency of the channel
  39. this.timer = new metrics.Timer(`event.${this.channel}.latency`)
  40. // keep a record of these objects to dispatch on
  41. CHANNEL_MANAGER[this.channel] = this
  42. }
  43. processEvent(id) {
  44. // if this is our event record it
  45. if (id === this.id) {
  46. this.count++
  47. if (this.timer != null) {
  48. this.timer.done()
  49. }
  50. return (this.timer = null) // only time the latency of the first event
  51. }
  52. }
  53. setStatus() {
  54. // if we saw the event anything other than a single time that is an error
  55. if (this.count !== 1) {
  56. logger.err(
  57. { channel: this.channel, count: this.count, id: this.id },
  58. 'redis channel health check error'
  59. )
  60. }
  61. const error = this.count !== 1
  62. return (CHANNEL_ERROR[this.channel] = error)
  63. }
  64. // class methods
  65. static check(channel, id) {
  66. // dispatch event to manager for channel
  67. return CHANNEL_MANAGER[channel] != null
  68. ? CHANNEL_MANAGER[channel].processEvent(id)
  69. : undefined
  70. }
  71. static status() {
  72. // return status of all channels for logging
  73. return CHANNEL_ERROR
  74. }
  75. static isFailing() {
  76. // check if any channel status is bad
  77. for (const channel in CHANNEL_ERROR) {
  78. const error = CHANNEL_ERROR[channel]
  79. if (error === true) {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. }