HealthChecker.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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('logger-sharelatex')
  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.log({ 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('no response from docstore with get check')
  49. } else if ((res != null ? res.statusCode : undefined) !== 200) {
  50. return cb(`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(`health check lines not equal ${body.lines} != ${lines}`)
  58. }
  59. })
  60. },
  61. cb => db.docs.deleteOne({ _id: doc_id, project_id }, cb),
  62. cb => db.docOps.deleteOne({ doc_id }, cb),
  63. ]
  64. return async.series(jobs, callback)
  65. },
  66. }