HealthCheckController.coffee 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. module.exports =
  9. check : (callback)->
  10. project_id = ObjectId()
  11. user_id = ObjectId(settings.notifications.healthCheck.user_id)
  12. notification_key = "smoke-test-notification"
  13. getOpts = (endPath)-> {url:"http://localhost:#{port}/user/#{user_id}#{endPath}", timeout:3000}
  14. logger.log user_id:user_id, opts:getOpts(), key:notification_key, user_id:user_id, "running health check"
  15. jobs = [
  16. (cb)->
  17. opts = getOpts("/")
  18. opts.json = {key: notification_key, messageOpts:'', templateKey:'', user_id:user_id}
  19. request.post(opts, cb)
  20. (cb)->
  21. opts = getOpts("/")
  22. opts.json = true
  23. request.get opts, (err, res, body)->
  24. if res.statusCode != 200
  25. return cb("status code not 200, its #{res.statusCode}")
  26. hasNotification = _.some body, (notification)->
  27. notification.key == notification_key and _.contains(notification.user_id, user_id.toString())
  28. if hasNotification
  29. cb()
  30. else
  31. cb("notification not found in response")
  32. ]
  33. async.series jobs, (err)->
  34. if err?
  35. callback(err)
  36. opts = getOpts("/notification_id/#{notification_id}")
  37. request.del opts, callback