HealthChecker.js 2.5 KB

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