overleaf/services/notifications/app/coffee/HealthCheckController.coffee

67 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2016-01-14 12:35:16 +00:00
ObjectId = require("mongojs").ObjectId
request = require("request")
async = require("async")
_ = require("underscore")
settings = require("settings-sharelatex")
port = settings.internal.notifications.port
logger = require "logger-sharelatex"
2016-01-14 20:42:48 +00:00
mongojs = require('mongojs')
Settings = require 'settings-sharelatex'
db = mongojs(Settings.mongo?.url, ['notifications'])
2016-01-14 12:35:16 +00:00
module.exports =
check : (callback)->
user_id = ObjectId()
2016-06-01 13:57:06 +00:00
cleanupNotifications = (callback)->
db.notifications.remove {user_id:user_id}, callback
2016-01-14 20:42:48 +00:00
notification_key = "smoke-test-notification-#{ObjectId()}"
getOpts = (endPath)-> {url:"http://localhost:#{port}/user/#{user_id}#{endPath}", timeout:5000}
logger.log user_id:user_id, opts:getOpts(), key:notification_key, user_id:user_id, "Health Check: running"
2016-01-14 12:35:16 +00:00
jobs = [
(cb)->
opts = getOpts("/")
2016-01-14 20:42:48 +00:00
opts.json = {key: notification_key, messageOpts:'', templateKey:'f4g5', user_id:user_id}
2016-01-14 12:35:16 +00:00
request.post(opts, cb)
(cb)->
opts = getOpts("/")
opts.json = true
request.get opts, (err, res, body)->
2016-06-01 13:34:23 +00:00
if err?
logger.err err:err, "Health Check: error getting notification"
return callback(err)
else if res.statusCode != 200
2016-02-04 13:44:17 +00:00
e = "status code not 200 #{res.statusCode}"
logger.err err:err, e
return cb(e)
2016-01-14 12:35:16 +00:00
hasNotification = _.some body, (notification)->
2016-01-14 20:42:48 +00:00
notification.key == notification_key and notification.user_id == user_id.toString()
2016-01-14 12:35:16 +00:00
if hasNotification
2016-02-04 13:44:17 +00:00
cb(null, body)
2016-01-14 12:35:16 +00:00
else
2016-06-01 13:21:49 +00:00
logger.err body:body, notification_key:notification_key, "Health Check: notification not in response"
2016-01-21 16:34:12 +00:00
return cb("notification not found in response")
2016-01-14 12:35:16 +00:00
]
2016-01-14 20:42:48 +00:00
async.series jobs, (err, body)->
2016-01-14 12:35:16 +00:00
if err?
logger.err err:err, "Health Check: error running health check"
2016-06-01 13:57:06 +00:00
cleanupNotifications ->
return callback(err)
2016-01-14 20:42:48 +00:00
else
notification_id = body[1][0]._id
2016-02-05 09:38:32 +00:00
notification_key = body[1][0].key
2016-01-14 20:42:48 +00:00
opts = getOpts("/notification/#{notification_id}")
logger.log notification_id:notification_id, notification_key:notification_key, "Health Check: doing cleanup"
2016-01-14 20:42:48 +00:00
request.del opts, (err, res, body)->
if err?
logger.err err, opts, "Health Check: error cleaning up notification"
return callback(err)
2016-02-05 09:38:32 +00:00
opts = getOpts("")
opts.json = {key: notification_key}
request.del opts, (err, res, body)->
if err?
logger.err err, opts, "Health Check: error cleaning up notification"
return callback(err)
2016-06-01 13:57:06 +00:00
cleanupNotifications callback