2015-10-16 05:13:54 -04:00
|
|
|
ObjectId = require("mongojs").ObjectId
|
|
|
|
request = require("request")
|
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
|
|
|
crypto = require("crypto")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
port = settings.internal.docstore.port
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
check : (callback)->
|
|
|
|
doc_id = ObjectId()
|
|
|
|
project_id = ObjectId(settings.docstore.healthCheck.project_id)
|
|
|
|
url = "http://localhost:#{port}/project/#{project_id}/doc/#{doc_id}"
|
|
|
|
lines = ["smoke test - delete me", "#{crypto.randomBytes(32).toString("hex")}"]
|
2015-10-16 07:57:05 -04:00
|
|
|
getOpts = -> {url:url, timeout:3000}
|
2015-10-16 05:13:54 -04:00
|
|
|
logger.log lines:lines, url:url, doc_id:doc_id, project_id:project_id, "running health check"
|
|
|
|
jobs = [
|
|
|
|
(cb)->
|
2015-10-16 07:30:53 -04:00
|
|
|
opts = getOpts()
|
|
|
|
opts.json = {lines: lines}
|
2015-10-16 05:13:54 -04:00
|
|
|
request.post(opts, cb)
|
|
|
|
(cb)->
|
2015-10-16 07:30:53 -04:00
|
|
|
opts = getOpts()
|
|
|
|
opts.json = true
|
|
|
|
request.get opts, (err, res, body)->
|
2015-12-01 16:50:18 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, "docstore returned a error in health check get"
|
|
|
|
cb(err)
|
2015-12-02 05:10:52 -05:00
|
|
|
else if !res?
|
|
|
|
cb("no response from docstore with get check")
|
2015-12-01 16:50:18 -05:00
|
|
|
else if res?.statusCode != 200
|
2015-10-16 05:13:54 -04:00
|
|
|
cb("status code not 200, its #{res.statusCode}")
|
2015-12-02 05:10:52 -05:00
|
|
|
else if _.isEqual(body?.lines, lines) and body?._id == doc_id.toString()
|
2015-10-16 05:13:54 -04:00
|
|
|
cb()
|
|
|
|
else
|
|
|
|
cb("health check lines not equal #{body.lines} != #{lines}")
|
|
|
|
]
|
2015-10-16 07:30:53 -04:00
|
|
|
async.series jobs, (err)->
|
|
|
|
if err?
|
|
|
|
callback(err)
|
|
|
|
request.del getOpts(), callback
|