HealthCheckController.coffee 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ObjectId = require("mongojs").ObjectId
  2. request = require("request")
  3. async = require("async")
  4. _ = require("underscore")
  5. settings = require("settings-sharelatex")
  6. port = settings.internal.notifications.port
  7. logger = require "logger-sharelatex"
  8. mongojs = require('mongojs')
  9. Settings = require 'settings-sharelatex'
  10. db = mongojs(Settings.mongo?.url, ['notifications'])
  11. module.exports =
  12. check : (callback)->
  13. user_id = ObjectId()
  14. cleanupNotifications = (callback)->
  15. db.notifications.remove {user_id:user_id}, callback
  16. notification_key = "smoke-test-notification-#{ObjectId()}"
  17. getOpts = (endPath)-> {url:"http://localhost:#{port}/user/#{user_id}#{endPath}", timeout:5000}
  18. logger.log user_id:user_id, opts:getOpts(), key:notification_key, user_id:user_id, "Health Check: running"
  19. jobs = [
  20. (cb)->
  21. opts = getOpts("/")
  22. opts.json = {key: notification_key, messageOpts:'', templateKey:'f4g5', user_id:user_id}
  23. request.post(opts, cb)
  24. (cb)->
  25. opts = getOpts("/")
  26. opts.json = true
  27. request.get opts, (err, res, body)->
  28. if err?
  29. logger.err err:err, "Health Check: error getting notification"
  30. return callback(err)
  31. else if res.statusCode != 200
  32. e = "status code not 200 #{res.statusCode}"
  33. logger.err err:err, e
  34. return cb(e)
  35. hasNotification = _.some body, (notification)->
  36. notification.key == notification_key and notification.user_id == user_id.toString()
  37. if hasNotification
  38. cb(null, body)
  39. else
  40. logger.err body:body, notification_key:notification_key, "Health Check: notification not in response"
  41. return cb("notification not found in response")
  42. ]
  43. async.series jobs, (err, body)->
  44. if err?
  45. logger.err err:err, "Health Check: error running health check"
  46. cleanupNotifications ->
  47. return callback(err)
  48. else
  49. notification_id = body[1][0]._id
  50. notification_key = body[1][0].key
  51. opts = getOpts("/notification/#{notification_id}")
  52. logger.log notification_id:notification_id, notification_key:notification_key, "Health Check: doing cleanup"
  53. request.del opts, (err, res, body)->
  54. if err?
  55. logger.err err, opts, "Health Check: error cleaning up notification"
  56. return callback(err)
  57. opts = getOpts("")
  58. opts.json = {key: notification_key}
  59. request.del opts, (err, res, body)->
  60. if err?
  61. logger.err err, opts, "Health Check: error cleaning up notification"
  62. return callback(err)
  63. cleanupNotifications callback