HealthChecker.js 2.4 KB

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