HealthCheckController.js 3.5 KB

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