HealthCheckController.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable
  2. camelcase,
  3. no-dupe-keys,
  4. standard/no-callback-literal,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { db, ObjectId } = require('./mongodb')
  15. const request = require('request')
  16. const async = require('async')
  17. const _ = require('underscore')
  18. const settings = require('@overleaf/settings')
  19. const { port } = settings.internal.notifications
  20. const logger = require('logger-sharelatex')
  21. module.exports = {
  22. check(callback) {
  23. const user_id = ObjectId()
  24. const cleanupNotifications = callback =>
  25. db.notifications.remove({ user_id }, callback)
  26. let notification_key = `smoke-test-notification-${ObjectId()}`
  27. const getOpts = endPath => ({
  28. url: `http://localhost:${port}/user/${user_id}${endPath}`,
  29. timeout: 5000,
  30. })
  31. logger.log(
  32. { user_id, opts: getOpts(), key: notification_key, user_id },
  33. 'Health Check: running'
  34. )
  35. const jobs = [
  36. function (cb) {
  37. const opts = getOpts('/')
  38. opts.json = {
  39. key: notification_key,
  40. messageOpts: '',
  41. templateKey: 'f4g5',
  42. user_id,
  43. }
  44. return request.post(opts, cb)
  45. },
  46. function (cb) {
  47. const opts = getOpts('/')
  48. opts.json = true
  49. return request.get(opts, function (err, res, body) {
  50. if (err != null) {
  51. logger.err({ err }, 'Health Check: error getting notification')
  52. return callback(err)
  53. } else if (res.statusCode !== 200) {
  54. const e = `status code not 200 ${res.statusCode}`
  55. logger.err({ err }, e)
  56. return cb(e)
  57. }
  58. const hasNotification = _.some(
  59. body,
  60. notification =>
  61. notification.key === notification_key &&
  62. notification.user_id === user_id.toString()
  63. )
  64. if (hasNotification) {
  65. return cb(null, body)
  66. } else {
  67. logger.err(
  68. { body, notification_key },
  69. 'Health Check: notification not in response'
  70. )
  71. return cb('notification not found in response')
  72. }
  73. })
  74. },
  75. ]
  76. return async.series(jobs, function (err, body) {
  77. if (err != null) {
  78. logger.err({ err }, 'Health Check: error running health check')
  79. return cleanupNotifications(() => callback(err))
  80. } else {
  81. const notification_id = body[1][0]._id
  82. notification_key = body[1][0].key
  83. let opts = getOpts(`/notification/${notification_id}`)
  84. logger.log(
  85. { notification_id, notification_key },
  86. 'Health Check: doing cleanup'
  87. )
  88. return request.del(opts, function (err, res, body) {
  89. if (err != null) {
  90. logger.err(
  91. err,
  92. opts,
  93. 'Health Check: error cleaning up notification'
  94. )
  95. return callback(err)
  96. }
  97. opts = getOpts('')
  98. opts.json = { key: notification_key }
  99. return request.del(opts, function (err, res, body) {
  100. if (err != null) {
  101. logger.err(
  102. err,
  103. opts,
  104. 'Health Check: error cleaning up notification'
  105. )
  106. return callback(err)
  107. }
  108. return cleanupNotifications(callback)
  109. })
  110. })
  111. }
  112. })
  113. },
  114. }