HealthChecker.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. import { ObjectId } from './mongodb.js'
  10. import request from 'request'
  11. import async from 'async'
  12. import settings from '@overleaf/settings'
  13. import logger from '@overleaf/logger'
  14. import OError from '@overleaf/o-error'
  15. import * as LockManager from './LockManager.js'
  16. const { port } = settings.internal.history
  17. export function check(callback) {
  18. const projectId = new ObjectId(settings.history.healthCheck.project_id)
  19. const url = `http://127.0.0.1:${port}/project/${projectId}`
  20. logger.debug({ projectId }, 'running health check')
  21. const jobs = [
  22. cb =>
  23. request.get(
  24. { url: `http://127.0.0.1:${port}/check_lock`, timeout: 3000 },
  25. function (err, res, body) {
  26. if (err != null) {
  27. OError.tag(err, 'error checking lock for health check', {
  28. project_id: projectId,
  29. })
  30. logger.err(err)
  31. return cb(err)
  32. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  33. return cb(new Error(`status code not 200, it's ${res.statusCode}`))
  34. } else {
  35. return cb()
  36. }
  37. }
  38. ),
  39. cb =>
  40. request.post(
  41. { url: `${url}/flush`, timeout: 10000 },
  42. function (err, res, body) {
  43. if (err != null) {
  44. OError.tag(err, 'error flushing for health check', {
  45. project_id: projectId,
  46. })
  47. logger.err(err)
  48. return cb(err)
  49. } else if ((res != null ? res.statusCode : undefined) !== 204) {
  50. return cb(new Error(`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. OError.tag(err, 'error getting updates for health check', {
  62. project_id: projectId,
  63. })
  64. logger.err(err)
  65. return cb(err)
  66. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  67. return cb(new Error(`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. export function checkLock(callback) {
  77. return LockManager.healthCheck(callback)
  78. }