overleaf/services/docstore/app/coffee/HealthChecker.coffee

44 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2018-10-03 09:57:01 -04:00
{db, ObjectId} = require "./mongojs"
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}
logger.log lines:lines, url:url, doc_id:doc_id, project_id:project_id, "running health check"
jobs = [
(cb)->
opts = getOpts()
2017-01-31 04:23:51 -05:00
opts.json = {lines: lines, version: 42, ranges: {}}
request.post(opts, cb)
(cb)->
opts = getOpts()
opts.json = true
request.get opts, (err, res, body)->
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")
else if res?.statusCode != 200
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()
cb()
else
cb("health check lines not equal #{body.lines} != #{lines}")
]
async.series jobs, (err)->
if err?
callback(err)
2018-10-03 09:57:01 -04:00
db.docs.remove {_id: doc_id, project_id: project_id}, callback