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

67 lines
2.4 KiB
JavaScript
Raw Normal View History

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