HealthChecker.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* eslint-disable
  2. camelcase,
  3. standard/no-callback-literal,
  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. const { ObjectId } = require('mongojs')
  14. const request = require('request')
  15. const async = require('async')
  16. const settings = require('settings-sharelatex')
  17. const { port } = settings.internal.trackchanges
  18. const logger = require('logger-sharelatex')
  19. const LockManager = require('./LockManager')
  20. module.exports = {
  21. check(callback) {
  22. const project_id = ObjectId(settings.trackchanges.healthCheck.project_id)
  23. const url = `http://localhost:${port}/project/${project_id}`
  24. logger.log({ project_id }, 'running health check')
  25. const jobs = [
  26. (cb) =>
  27. request.get(
  28. { url: `http://localhost:${port}/check_lock`, timeout: 3000 },
  29. function (err, res, body) {
  30. if (err != null) {
  31. logger.err(
  32. { err, project_id },
  33. 'error checking lock for health check'
  34. )
  35. return cb(err)
  36. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  37. return cb(`status code not 200, it's ${res.statusCode}`)
  38. } else {
  39. return cb()
  40. }
  41. }
  42. ),
  43. (cb) =>
  44. request.post({ url: `${url}/flush`, timeout: 10000 }, function (
  45. err,
  46. res,
  47. body
  48. ) {
  49. if (err != null) {
  50. logger.err({ err, project_id }, 'error flushing for health check')
  51. return cb(err)
  52. } else if ((res != null ? res.statusCode : undefined) !== 204) {
  53. return cb(`status code not 204, it's ${res.statusCode}`)
  54. } else {
  55. return cb()
  56. }
  57. }),
  58. (cb) =>
  59. request.get({ url: `${url}/updates`, timeout: 10000 }, function (
  60. err,
  61. res,
  62. body
  63. ) {
  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(`status code not 200, it's ${res.statusCode}`)
  72. } else {
  73. return cb()
  74. }
  75. })
  76. ]
  77. return async.series(jobs, callback)
  78. },
  79. checkLock(callback) {
  80. return LockManager.healthCheck(callback)
  81. }
  82. }