HealthChecker.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('logger-sharelatex')
  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.log({ 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(`status code not 200, it's ${res.statusCode}`)
  37. } else {
  38. return cb()
  39. }
  40. }
  41. ),
  42. cb =>
  43. request.post(
  44. { url: `${url}/flush`, timeout: 10000 },
  45. function (err, res, body) {
  46. if (err != null) {
  47. logger.err({ err, project_id }, 'error flushing for health check')
  48. return cb(err)
  49. } else if ((res != null ? res.statusCode : undefined) !== 204) {
  50. return cb(`status code not 204, it's ${res.statusCode}`)
  51. } else {
  52. return cb()
  53. }
  54. }
  55. ),
  56. cb =>
  57. request.get(
  58. { url: `${url}/updates`, timeout: 10000 },
  59. function (err, res, body) {
  60. if (err != null) {
  61. logger.err(
  62. { err, project_id },
  63. 'error getting updates for health check'
  64. )
  65. return cb(err)
  66. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  67. return cb(`status code not 200, it's ${res.statusCode}`)
  68. } else {
  69. return cb()
  70. }
  71. }
  72. ),
  73. ]
  74. return async.series(jobs, callback)
  75. },
  76. checkLock(callback) {
  77. return LockManager.healthCheck(callback)
  78. },
  79. }