HealthCheckController.js 3.6 KB

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