HealthChecker.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. import { ObjectId } from 'mongodb'
  13. import request from 'request'
  14. import async from 'async'
  15. import settings from '@overleaf/settings'
  16. import logger from '@overleaf/logger'
  17. import OError from '@overleaf/o-error'
  18. import * as LockManager from './LockManager.js'
  19. const { port } = settings.internal.history
  20. export function check(callback) {
  21. const project_id = ObjectId(settings.history.healthCheck.project_id)
  22. const url = `http://localhost:${port}/project/${project_id}`
  23. logger.debug({ 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. OError.tag(err, 'error checking lock for health check', {
  31. project_id,
  32. })
  33. logger.err(err)
  34. return cb(err)
  35. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  36. return cb(new Error(`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. OError.tag(err, 'error flushing for health check', { project_id })
  48. logger.err(err)
  49. return cb(err)
  50. } else if ((res != null ? res.statusCode : undefined) !== 204) {
  51. return cb(new Error(`status code not 204, it's ${res.statusCode}`))
  52. } else {
  53. return cb()
  54. }
  55. }
  56. ),
  57. cb =>
  58. request.get(
  59. { url: `${url}/updates`, timeout: 10000 },
  60. function (err, res, body) {
  61. if (err != null) {
  62. OError.tag(err, 'error getting updates for health check', {
  63. project_id,
  64. })
  65. logger.err(err)
  66. return cb(err)
  67. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  68. return cb(new Error(`status code not 200, it's ${res.statusCode}`))
  69. } else {
  70. return cb()
  71. }
  72. }
  73. ),
  74. ]
  75. return async.series(jobs, callback)
  76. }
  77. export function checkLock(callback) {
  78. return LockManager.healthCheck(callback)
  79. }