HealthChecker.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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('./mongodb')
  14. const request = require('request')
  15. const async = require('async')
  16. const settings = require('@overleaf/settings')
  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(
  45. { url: `${url}/flush`, timeout: 10000 },
  46. function (err, res, body) {
  47. if (err != null) {
  48. logger.err({ err, project_id }, 'error flushing for health check')
  49. return cb(err)
  50. } else if ((res != null ? res.statusCode : undefined) !== 204) {
  51. return cb(`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. logger.err(
  63. { err, project_id },
  64. 'error getting updates for health check'
  65. )
  66. return cb(err)
  67. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  68. return cb(`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. checkLock(callback) {
  78. return LockManager.healthCheck(callback)
  79. },
  80. }