HealthChecker.js 2.7 KB

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