HealthCheckController.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. parent = smokeTestModule.parent
  24. while (idx = parent.children.indexOf(smokeTestModule)) != -1
  25. parent.children.splice(idx, 1)
  26. # remove the smokeTest from the module cache
  27. delete require.cache[path]
  28. checkRedis: (req, res, next)->
  29. if redisCheck.isAlive()
  30. res.send 200
  31. else
  32. res.send 500
  33. Reporter = (res) ->
  34. (runner) ->
  35. Base.call(this, runner)
  36. tests = []
  37. passes = []
  38. failures = []
  39. runner.on 'test end', (test) -> tests.push(test)
  40. runner.on 'pass', (test) -> passes.push(test)
  41. runner.on 'fail', (test) -> failures.push(test)
  42. runner.on 'end', () =>
  43. clean = (test) ->
  44. title: test.fullTitle()
  45. duration: test.duration
  46. err: test.err
  47. timedOut: test.timedOut
  48. results = {
  49. stats: @stats
  50. failures: failures.map(clean)
  51. passes: passes.map(clean)
  52. }
  53. res.contentType("application/json")
  54. if failures.length > 0
  55. res.send 500, JSON.stringify(results, null, 2)
  56. else
  57. res.send 200, JSON.stringify(results, null, 2)