HealthChecker.js 2.3 KB

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