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