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

45 lines
1.5 KiB
JavaScript
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}")
2018-10-19 06:37:44 -04:00
(cb)->
db.docs.remove {_id: doc_id, project_id: project_id}, cb
(cb)->
db.docOps.remove {doc_id: doc_id}, cb
]
2018-10-19 06:37:44 -04:00
async.series jobs, callback