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

47 lines
1.6 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(settings.notifications.healthCheck.user_id)
2016-01-14 20:42:48 +00:00
notification_key = "smoke-test-notification-#{ObjectId()}"
2016-01-14 12:35:16 +00:00
getOpts = (endPath)-> {url:"http://localhost:#{port}/user/#{user_id}#{endPath}", timeout:3000}
logger.log user_id:user_id, opts:getOpts(), key:notification_key, user_id:user_id, "running health check"
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)->
if res.statusCode != 200
return cb("status code not 200, its #{res.statusCode}")
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-01-14 20:42:48 +00:00
cb(null,body)
2016-01-14 12:35:16 +00:00
else
2016-01-14 20:42:48 +00:00
logger.log body:body, "what is in the body"
2016-01-14 12:35:16 +00:00
cb("notification not found in response")
]
2016-01-14 20:42:48 +00:00
async.series jobs, (err, body)->
2016-01-14 12:35:16 +00:00
if err?
callback(err)
2016-01-14 20:42:48 +00:00
else
notification_id = body[1][0]._id
opts = getOpts("/notification/#{notification_id}")
request.del opts, (err, res, body)->
db.notifications.remove {_id:ObjectId(notification_id)}, callback