HealthChecker.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const { db, ObjectId } = require('./mongodb')
  10. const request = require('request')
  11. const async = require('async')
  12. const _ = require('lodash')
  13. const crypto = require('node:crypto')
  14. const settings = require('@overleaf/settings')
  15. const { port } = settings.internal.docstore
  16. const logger = require('@overleaf/logger')
  17. module.exports = {
  18. check(callback) {
  19. const docId = new ObjectId()
  20. const projectId = new ObjectId(settings.docstore.healthCheck.project_id)
  21. const url = `http://127.0.0.1:${port}/project/${projectId}/doc/${docId}`
  22. const lines = [
  23. 'smoke test - delete me',
  24. `${crypto.randomBytes(32).toString('hex')}`,
  25. ]
  26. const getOpts = () => ({
  27. url,
  28. timeout: 3000,
  29. })
  30. logger.debug({ lines, url, docId, projectId }, 'running health check')
  31. const jobs = [
  32. function (cb) {
  33. const opts = getOpts()
  34. opts.json = { lines, version: 42, ranges: {} }
  35. return request.post(opts, cb)
  36. },
  37. function (cb) {
  38. const opts = getOpts()
  39. opts.json = true
  40. return request.get(opts, function (err, res, body) {
  41. if (err != null) {
  42. logger.err({ err }, 'docstore returned a error in health check get')
  43. return cb(err)
  44. } else if (res == null) {
  45. return cb(new Error('no response from docstore with get check'))
  46. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  47. return cb(new Error(`status code not 200, its ${res.statusCode}`))
  48. } else if (
  49. _.isEqual(body != null ? body.lines : undefined, lines) &&
  50. (body != null ? body._id : undefined) === docId.toString()
  51. ) {
  52. return cb()
  53. } else {
  54. return cb(
  55. new Error(
  56. `health check lines not equal ${body.lines} != ${lines}`
  57. )
  58. )
  59. }
  60. })
  61. },
  62. cb => db.docs.deleteOne({ _id: docId, project_id: projectId }, cb),
  63. ]
  64. return async.series(jobs, callback)
  65. },
  66. }