HealthCheckController.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable
  2. no-dupe-keys,
  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 settings = require('@overleaf/settings')
  16. const { port } = settings.internal.notifications
  17. const logger = require('@overleaf/logger')
  18. module.exports = {
  19. check(callback) {
  20. const userId = new ObjectId()
  21. const cleanupNotifications = callback =>
  22. db.notifications.deleteOne({ user_id: userId }, callback)
  23. let notificationKey = `smoke-test-notification-${new ObjectId()}`
  24. const getOpts = endPath => ({
  25. url: `http://127.0.0.1:${port}/user/${userId}${endPath}`,
  26. timeout: 5000,
  27. })
  28. logger.debug(
  29. { userId, opts: getOpts(), key: notificationKey, userId },
  30. 'Health Check: running'
  31. )
  32. const jobs = [
  33. function (cb) {
  34. const opts = getOpts('/')
  35. opts.json = {
  36. key: notificationKey,
  37. messageOpts: '',
  38. templateKey: 'f4g5',
  39. user_id: userId,
  40. }
  41. return request.post(opts, cb)
  42. },
  43. function (cb) {
  44. const opts = getOpts('/')
  45. opts.json = true
  46. return request.get(opts, function (err, res, body) {
  47. if (err != null) {
  48. logger.err({ err }, 'Health Check: error getting notification')
  49. return callback(err)
  50. } else if (res.statusCode !== 200) {
  51. const e = `status code not 200 ${res.statusCode}`
  52. logger.err({ err }, e)
  53. return cb(e)
  54. }
  55. const hasNotification = body.some(
  56. notification =>
  57. notification.key === notificationKey &&
  58. notification.user_id === userId.toString()
  59. )
  60. if (hasNotification) {
  61. return cb(null, body)
  62. } else {
  63. logger.err(
  64. { body, notificationKey },
  65. 'Health Check: notification not in response'
  66. )
  67. return cb(new Error('notification not found in response'))
  68. }
  69. })
  70. },
  71. ]
  72. return async.series(jobs, function (err, body) {
  73. if (err != null) {
  74. logger.err({ err }, 'Health Check: error running health check')
  75. return cleanupNotifications(() => callback(err))
  76. } else {
  77. const notificationId = body[1][0]._id
  78. notificationKey = body[1][0].key
  79. let opts = getOpts(`/notification/${notificationId}`)
  80. logger.debug(
  81. { notificationId, notificationKey },
  82. 'Health Check: doing cleanup'
  83. )
  84. return request.del(opts, function (err, res, body) {
  85. if (err != null) {
  86. logger.err(
  87. err,
  88. opts,
  89. 'Health Check: error cleaning up notification'
  90. )
  91. return callback(err)
  92. }
  93. opts = getOpts('')
  94. opts.json = { key: notificationKey }
  95. return request.del(opts, function (err, res, body) {
  96. if (err != null) {
  97. logger.err(
  98. err,
  99. opts,
  100. 'Health Check: error cleaning up notification'
  101. )
  102. return callback(err)
  103. }
  104. return cleanupNotifications(callback)
  105. })
  106. })
  107. }
  108. })
  109. },
  110. }