2020-02-16 09:01:47 -05:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-16 09:01:46 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-08-28 08:13:19 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2020-02-16 09:02:21 -05:00
|
|
|
const request = require('request')
|
|
|
|
const async = require('async')
|
2022-12-15 07:53:03 -05:00
|
|
|
const _ = require('lodash')
|
2024-11-08 05:21:56 -05:00
|
|
|
const crypto = require('node:crypto')
|
2021-07-12 12:47:20 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-02-16 09:02:21 -05:00
|
|
|
const { port } = settings.internal.docstore
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2015-10-16 05:13:54 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
module.exports = {
|
|
|
|
check(callback) {
|
2023-12-13 04:38:54 -05:00
|
|
|
const docId = new ObjectId()
|
|
|
|
const projectId = new ObjectId(settings.docstore.healthCheck.project_id)
|
2024-04-25 08:56:00 -04:00
|
|
|
const url = `http://127.0.0.1:${port}/project/${projectId}/doc/${docId}`
|
2020-02-16 09:02:21 -05:00
|
|
|
const lines = [
|
|
|
|
'smoke test - delete me',
|
2021-07-13 07:04:48 -04:00
|
|
|
`${crypto.randomBytes(32).toString('hex')}`,
|
2020-02-16 09:02:21 -05:00
|
|
|
]
|
|
|
|
const getOpts = () => ({
|
|
|
|
url,
|
2021-07-13 07:04:48 -04:00
|
|
|
timeout: 3000,
|
2020-02-16 09:02:21 -05:00
|
|
|
})
|
2023-03-16 07:58:12 -04:00
|
|
|
logger.debug({ lines, url, docId, projectId }, 'running health check')
|
2020-02-16 09:02:21 -05:00
|
|
|
const jobs = [
|
2020-05-28 09:20:54 -04:00
|
|
|
function (cb) {
|
2020-02-16 09:02:21 -05:00
|
|
|
const opts = getOpts()
|
|
|
|
opts.json = { lines, version: 42, ranges: {} }
|
|
|
|
return request.post(opts, cb)
|
|
|
|
},
|
2020-05-28 09:20:54 -04:00
|
|
|
function (cb) {
|
2020-02-16 09:02:21 -05:00
|
|
|
const opts = getOpts()
|
|
|
|
opts.json = true
|
2020-05-28 09:20:54 -04:00
|
|
|
return request.get(opts, function (err, res, body) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'docstore returned a error in health check get')
|
|
|
|
return cb(err)
|
|
|
|
} else if (res == null) {
|
2021-10-27 05:49:42 -04:00
|
|
|
return cb(new Error('no response from docstore with get check'))
|
2020-02-16 09:02:21 -05:00
|
|
|
} else if ((res != null ? res.statusCode : undefined) !== 200) {
|
2021-10-27 05:49:42 -04:00
|
|
|
return cb(new Error(`status code not 200, its ${res.statusCode}`))
|
2020-02-16 09:02:21 -05:00
|
|
|
} else if (
|
|
|
|
_.isEqual(body != null ? body.lines : undefined, lines) &&
|
2023-03-16 07:58:12 -04:00
|
|
|
(body != null ? body._id : undefined) === docId.toString()
|
2020-02-16 09:02:21 -05:00
|
|
|
) {
|
|
|
|
return cb()
|
|
|
|
} else {
|
2021-10-27 05:49:42 -04:00
|
|
|
return cb(
|
|
|
|
new Error(
|
|
|
|
`health check lines not equal ${body.lines} != ${lines}`
|
|
|
|
)
|
|
|
|
)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2023-03-16 07:58:12 -04:00
|
|
|
cb => db.docs.deleteOne({ _id: docId, project_id: projectId }, cb),
|
2020-02-16 09:02:21 -05:00
|
|
|
]
|
|
|
|
return async.series(jobs, callback)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|