HealthCheckController.js 3.8 KB

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