HealthChecker.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* eslint-disable
  2. camelcase,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const { ObjectId } = require('./mongodb')
  13. const request = require('request')
  14. const async = require('async')
  15. const settings = require('@overleaf/settings')
  16. const { port } = settings.internal.trackchanges
  17. const logger = require('@overleaf/logger')
  18. const LockManager = require('./LockManager')
  19. module.exports = {
  20. check(callback) {
  21. const project_id = ObjectId(settings.trackchanges.healthCheck.project_id)
  22. const url = `http://localhost:${port}/project/${project_id}`
  23. logger.debug({ project_id }, 'running health check')
  24. const jobs = [
  25. cb =>
  26. request.get(
  27. { url: `http://localhost:${port}/check_lock`, timeout: 3000 },
  28. function (err, res, body) {
  29. if (err != null) {
  30. logger.err(
  31. { err, project_id },
  32. 'error checking lock for health check'
  33. )
  34. return cb(err)
  35. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  36. return cb(
  37. new Error(`status code not 200, it's ${res.statusCode}`)
  38. )
  39. } else {
  40. return cb()
  41. }
  42. }
  43. ),
  44. cb =>
  45. request.post(
  46. { url: `${url}/flush`, timeout: 10000 },
  47. function (err, res, body) {
  48. if (err != null) {
  49. logger.err({ err, project_id }, 'error flushing for health check')
  50. return cb(err)
  51. } else if ((res != null ? res.statusCode : undefined) !== 204) {
  52. return cb(
  53. new Error(`status code not 204, it's ${res.statusCode}`)
  54. )
  55. } else {
  56. return cb()
  57. }
  58. }
  59. ),
  60. cb =>
  61. request.get(
  62. { url: `${url}/updates`, timeout: 10000 },
  63. function (err, res, body) {
  64. if (err != null) {
  65. logger.err(
  66. { err, project_id },
  67. 'error getting updates for health check'
  68. )
  69. return cb(err)
  70. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  71. return cb(
  72. new Error(`status code not 200, it's ${res.statusCode}`)
  73. )
  74. } else {
  75. return cb()
  76. }
  77. }
  78. ),
  79. ]
  80. return async.series(jobs, callback)
  81. },
  82. checkLock(callback) {
  83. return LockManager.healthCheck(callback)
  84. },
  85. }