2014-02-12 10:23:40 +00:00
|
|
|
Mocha = require "mocha"
|
|
|
|
Base = require("mocha/lib/reporters/base")
|
2014-11-19 23:18:20 +00:00
|
|
|
redis = require("redis-sharelatex")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
redisCheck = redis.activeHealthCheckRedis(settings.redis.web)
|
2014-11-24 13:36:10 +00:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
domain = require "domain"
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
module.exports = HealthCheckController =
|
|
|
|
check: (req, res, next = (error) ->) ->
|
2014-11-24 13:36:10 +00:00
|
|
|
d = domain.create()
|
|
|
|
d.on "error", (error) ->
|
|
|
|
logger.err err: error, "error in mocha"
|
|
|
|
d.run () ->
|
|
|
|
mocha = new Mocha(reporter: Reporter(res), timeout: 10000)
|
|
|
|
mocha.addFile("test/smoke/js/SmokeTests.js")
|
|
|
|
mocha.run () ->
|
2015-05-26 09:54:55 +00:00
|
|
|
# TODO: combine this with the smoke-test-sharelatex module
|
|
|
|
# we need to clean up all references to the smokeTest module
|
|
|
|
# so it can be garbage collected. The only reference should
|
|
|
|
# be in its parent, when it is loaded by mocha.addFile.
|
2014-11-24 13:36:10 +00:00
|
|
|
path = require.resolve(__dirname + "/../../../../test/smoke/js/SmokeTests.js")
|
2015-05-26 09:54:55 +00:00
|
|
|
smokeTestModule = require.cache[path]
|
2015-05-26 15:33:02 +00:00
|
|
|
if smokeTestModule?
|
|
|
|
parent = smokeTestModule.parent
|
|
|
|
while (idx = parent.children.indexOf(smokeTestModule)) != -1
|
|
|
|
parent.children.splice(idx, 1)
|
|
|
|
else
|
|
|
|
logger.warn {path}, "smokeTestModule not defined"
|
2015-05-26 09:54:55 +00:00
|
|
|
# remove the smokeTest from the module cache
|
2014-11-24 13:36:10 +00:00
|
|
|
delete require.cache[path]
|
2014-11-19 23:18:20 +00:00
|
|
|
|
|
|
|
checkRedis: (req, res, next)->
|
|
|
|
if redisCheck.isAlive()
|
2015-07-08 15:56:38 +00:00
|
|
|
res.sendStatus 200
|
2014-11-19 23:18:20 +00:00
|
|
|
else
|
2015-07-08 15:56:38 +00:00
|
|
|
res.sendStatus 500
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
Reporter = (res) ->
|
|
|
|
(runner) ->
|
|
|
|
Base.call(this, runner)
|
|
|
|
|
|
|
|
tests = []
|
|
|
|
passes = []
|
|
|
|
failures = []
|
|
|
|
|
|
|
|
runner.on 'test end', (test) -> tests.push(test)
|
|
|
|
runner.on 'pass', (test) -> passes.push(test)
|
|
|
|
runner.on 'fail', (test) -> failures.push(test)
|
|
|
|
|
|
|
|
runner.on 'end', () =>
|
|
|
|
clean = (test) ->
|
|
|
|
title: test.fullTitle()
|
|
|
|
duration: test.duration
|
|
|
|
err: test.err
|
|
|
|
timedOut: test.timedOut
|
|
|
|
|
|
|
|
results = {
|
|
|
|
stats: @stats
|
|
|
|
failures: failures.map(clean)
|
|
|
|
passes: passes.map(clean)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.contentType("application/json")
|
|
|
|
if failures.length > 0
|
2016-10-21 17:59:28 +00:00
|
|
|
logger.err failures:failures, "health check failed"
|
2014-02-12 10:23:40 +00:00
|
|
|
res.send 500, JSON.stringify(results, null, 2)
|
|
|
|
else
|
|
|
|
res.send 200, JSON.stringify(results, null, 2)
|
|
|
|
|