HealthCheckController.coffee 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Mocha = require "mocha"
  2. Base = require("mocha/lib/reporters/base")
  3. redis = require("redis-sharelatex")
  4. settings = require("settings-sharelatex")
  5. redisCheck = redis.activeHealthCheckRedis(settings.redis.web)
  6. logger = require "logger-sharelatex"
  7. domain = require "domain"
  8. module.exports = HealthCheckController =
  9. check: (req, res, next = (error) ->) ->
  10. d = domain.create()
  11. d.on "error", (error) ->
  12. logger.err err: error, "error in mocha"
  13. d.run () ->
  14. mocha = new Mocha(reporter: Reporter(res), timeout: 10000)
  15. mocha.addFile("test/smoke/js/SmokeTests.js")
  16. mocha.run () ->
  17. # TODO: combine this with the smoke-test-sharelatex module
  18. # we need to clean up all references to the smokeTest module
  19. # so it can be garbage collected. The only reference should
  20. # be in its parent, when it is loaded by mocha.addFile.
  21. path = require.resolve(__dirname + "/../../../../test/smoke/js/SmokeTests.js")
  22. smokeTestModule = require.cache[path]
  23. if smokeTestModule?
  24. parent = smokeTestModule.parent
  25. while (idx = parent.children.indexOf(smokeTestModule)) != -1
  26. parent.children.splice(idx, 1)
  27. else
  28. logger.warn {path}, "smokeTestModule not defined"
  29. # remove the smokeTest from the module cache
  30. delete require.cache[path]
  31. checkRedis: (req, res, next)->
  32. if redisCheck.isAlive()
  33. res.sendStatus 200
  34. else
  35. res.sendStatus 500
  36. Reporter = (res) ->
  37. (runner) ->
  38. Base.call(this, runner)
  39. tests = []
  40. passes = []
  41. failures = []
  42. runner.on 'test end', (test) -> tests.push(test)
  43. runner.on 'pass', (test) -> passes.push(test)
  44. runner.on 'fail', (test) -> failures.push(test)
  45. runner.on 'end', () =>
  46. clean = (test) ->
  47. title: test.fullTitle()
  48. duration: test.duration
  49. err: test.err
  50. timedOut: test.timedOut
  51. results = {
  52. stats: @stats
  53. failures: failures.map(clean)
  54. passes: passes.map(clean)
  55. }
  56. res.contentType("application/json")
  57. if failures.length > 0
  58. res.send 500, JSON.stringify(results, null, 2)
  59. else
  60. res.send 200, JSON.stringify(results, null, 2)