HealthCheckController.js 3.5 KB

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