diff --git a/services/track-changes/app.coffee b/services/track-changes/app.coffee index 1600404963..96423b3166 100644 --- a/services/track-changes/app.coffee +++ b/services/track-changes/app.coffee @@ -47,6 +47,8 @@ app.post "/pack", (req, res, next) -> app.get "/status", (req, res, next) -> res.send "track-changes is alive" +app.get "/health_check", HttpController.healthCheck + app.use (error, req, res, next) -> logger.error err: error, "an internal error occured" res.send 500 diff --git a/services/track-changes/app/coffee/HealthChecker.coffee b/services/track-changes/app/coffee/HealthChecker.coffee new file mode 100644 index 0000000000..efa3c2335d --- /dev/null +++ b/services/track-changes/app/coffee/HealthChecker.coffee @@ -0,0 +1,31 @@ +ObjectId = require("mongojs").ObjectId +request = require("request") +async = require("async") +settings = require("settings-sharelatex") +port = settings.internal.trackchanges.port +logger = require "logger-sharelatex" + +module.exports = + check : (callback)-> + project_id = ObjectId(settings.trackchanges.healthCheck.project_id) + url = "http://localhost:#{port}/project/#{project_id}" + logger.log project_id:project_id, "running health check" + jobs = [ + (cb)-> + request.post {url:"#{url}/flush", timeout:3000}, (err, res, body) -> + if err? + cb(err) + else if res?.statusCode != 204 + cb("status code not 204, it's #{res.statusCode}") + else + cb() + (cb)-> + request.get {url:"#{url}/updates", timeout:3000}, (err, res, body)-> + if err? + cb(err) + else if res?.statusCode != 200 + cb("status code not 200, it's #{res.statusCode}") + else + cb() + ] + async.series jobs, callback diff --git a/services/track-changes/app/coffee/HttpController.coffee b/services/track-changes/app/coffee/HttpController.coffee index 298311c180..c91eb802bf 100644 --- a/services/track-changes/app/coffee/HttpController.coffee +++ b/services/track-changes/app/coffee/HttpController.coffee @@ -4,6 +4,7 @@ PackManager = require "./PackManager" RestoreManager = require "./RestoreManager" logger = require "logger-sharelatex" DocArchiveManager = require "./DocArchiveManager" +HealthChecker = require "./HealthChecker" module.exports = HttpController = flushDoc: (req, res, next = (error) ->) -> @@ -80,4 +81,12 @@ module.exports = HttpController = logger.log project_id: project_id, "unarchiving all track changes from s3" DocArchiveManager.unArchiveAllDocsChanges project_id, (error) -> return next(error) if error? - res.send 204 \ No newline at end of file + res.send 204 + + healthCheck: (req, res)-> + HealthChecker.check (err)-> + if err? + logger.err err:err, "error performing health check" + res.send 500 + else + res.send 200 diff --git a/services/track-changes/package.json b/services/track-changes/package.json index 4fe112262a..7c14dd8f28 100644 --- a/services/track-changes/package.json +++ b/services/track-changes/package.json @@ -16,7 +16,7 @@ "logger-sharelatex": "git+https://github.com/sharelatex/logger-sharelatex.git#v1.0.0", "metrics-sharelatex": "git+https://github.com/sharelatex/metrics-sharelatex.git#v1.0.0", "request": "~2.33.0", - "redis-sharelatex": "~0.0.4", + "redis-sharelatex": "~0.0.9", "redis": "~0.10.1", "underscore": "~1.7.0", "mongo-uri": "^0.1.2",