2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
const request = require('request')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const metrics = require('metrics-sharelatex')
|
2020-08-20 06:30:22 -04:00
|
|
|
const { NullBytesInOpError, UpdateTooLargeError } = require('./Errors')
|
2014-11-12 10:54:55 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const rclient = require('redis-sharelatex').createClient(
|
|
|
|
settings.redis.documentupdater
|
|
|
|
)
|
|
|
|
const Keys = settings.redis.documentupdater.key_schema
|
2014-11-13 12:07:05 -05:00
|
|
|
|
2020-07-13 05:42:50 -04:00
|
|
|
const DocumentUpdaterManager = {
|
2020-06-23 13:29:44 -04:00
|
|
|
getDocument(project_id, doc_id, fromVersion, callback) {
|
|
|
|
const timer = new metrics.Timer('get-document')
|
|
|
|
const url = `${settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}?fromVersion=${fromVersion}`
|
|
|
|
logger.log(
|
|
|
|
{ project_id, doc_id, fromVersion },
|
|
|
|
'getting doc from document updater'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
request.get(url, function (err, res, body) {
|
2020-06-23 13:29:44 -04:00
|
|
|
timer.done()
|
2020-07-07 06:06:02 -04:00
|
|
|
if (err) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.error(
|
|
|
|
{ err, url, project_id, doc_id },
|
|
|
|
'error getting doc from doc updater'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
logger.log(
|
|
|
|
{ project_id, doc_id },
|
|
|
|
'got doc from document document updater'
|
|
|
|
)
|
|
|
|
try {
|
|
|
|
body = JSON.parse(body)
|
|
|
|
} catch (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
body = body || {}
|
|
|
|
callback(null, body.lines, body.version, body.ranges, body.ops)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else if ([404, 422].includes(res.statusCode)) {
|
|
|
|
err = new Error('doc updater could not load requested ops')
|
|
|
|
err.statusCode = res.statusCode
|
|
|
|
logger.warn(
|
|
|
|
{ err, project_id, doc_id, url, fromVersion },
|
|
|
|
'doc updater could not load requested ops'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else {
|
|
|
|
err = new Error(
|
|
|
|
`doc updater returned a non-success status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
err.statusCode = res.statusCode
|
|
|
|
logger.error(
|
|
|
|
{ err, project_id, doc_id, url },
|
|
|
|
`doc updater returned a non-success status code: ${res.statusCode}`
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2014-11-14 10:53:59 -05:00
|
|
|
|
2020-07-13 05:42:50 -04:00
|
|
|
checkDocument(project_id, doc_id, callback) {
|
|
|
|
// in this call fromVersion = -1 means get document without docOps
|
|
|
|
DocumentUpdaterManager.getDocument(project_id, doc_id, -1, callback)
|
|
|
|
},
|
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
flushProjectToMongoAndDelete(project_id, callback) {
|
|
|
|
// this method is called when the last connected user leaves the project
|
|
|
|
logger.log({ project_id }, 'deleting project from document updater')
|
|
|
|
const timer = new metrics.Timer('delete.mongo.project')
|
|
|
|
// flush the project in the background when all users have left
|
|
|
|
const url =
|
|
|
|
`${settings.apis.documentupdater.url}/project/${project_id}?background=true` +
|
|
|
|
(settings.shutDownInProgress ? '&shutdown=true' : '')
|
2020-07-07 06:06:02 -04:00
|
|
|
request.del(url, function (err, res) {
|
2020-06-23 13:29:44 -04:00
|
|
|
timer.done()
|
2020-07-07 06:06:02 -04:00
|
|
|
if (err) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.error(
|
|
|
|
{ err, project_id },
|
|
|
|
'error deleting project from document updater'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
logger.log({ project_id }, 'deleted project from document updater')
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(null)
|
2020-06-23 13:29:44 -04:00
|
|
|
} else {
|
|
|
|
err = new Error(
|
|
|
|
`document updater returned a failure status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
err.statusCode = res.statusCode
|
|
|
|
logger.error(
|
|
|
|
{ err, project_id },
|
|
|
|
`document updater returned failure status code: ${res.statusCode}`
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2019-05-31 17:27:05 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
queueChange(project_id, doc_id, change, callback) {
|
|
|
|
const allowedKeys = [
|
|
|
|
'doc',
|
|
|
|
'op',
|
|
|
|
'v',
|
|
|
|
'dupIfSource',
|
|
|
|
'meta',
|
|
|
|
'lastV',
|
|
|
|
'hash'
|
|
|
|
]
|
|
|
|
change = _.pick(change, allowedKeys)
|
|
|
|
const jsonChange = JSON.stringify(change)
|
|
|
|
if (jsonChange.indexOf('\u0000') !== -1) {
|
|
|
|
// memory corruption check
|
2020-08-20 06:30:22 -04:00
|
|
|
return callback(new NullBytesInOpError(jsonChange))
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-03-24 06:22:28 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const updateSize = jsonChange.length
|
|
|
|
if (updateSize > settings.maxUpdateSize) {
|
2020-08-20 06:16:26 -04:00
|
|
|
return callback(new UpdateTooLargeError(updateSize))
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-03-24 06:22:28 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// record metric for each update added to queue
|
|
|
|
metrics.summary('redis.pendingUpdates', updateSize, { status: 'push' })
|
2020-04-06 11:24:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
const doc_key = `${project_id}:${doc_id}`
|
|
|
|
// Push onto pendingUpdates for doc_id first, because once the doc updater
|
|
|
|
// gets an entry on pending-updates-list, it starts processing.
|
2020-07-07 06:06:02 -04:00
|
|
|
rclient.rpush(Keys.pendingUpdates({ doc_id }), jsonChange, function (
|
2020-06-23 13:29:44 -04:00
|
|
|
error
|
|
|
|
) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
rclient.rpush('pending-updates-list', doc_key, callback)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 05:42:50 -04:00
|
|
|
|
|
|
|
module.exports = DocumentUpdaterManager
|