2020-05-06 06:09:15 -04:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04: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-05-06 06:09:33 -04:00
|
|
|
let HistoryManager
|
|
|
|
const async = require('async')
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
const request = require('request')
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-05-06 06:09:33 -04:00
|
|
|
const HistoryRedisManager = require('./HistoryRedisManager')
|
|
|
|
const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
|
|
|
|
const RedisManager = require('./RedisManager')
|
|
|
|
const metrics = require('./Metrics')
|
2016-11-28 05:14:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
module.exports = HistoryManager = {
|
2023-03-21 08:06:13 -04:00
|
|
|
flushDocChangesAsync(projectId, docId) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
(Settings.apis != null ? Settings.apis.trackchanges : undefined) == null
|
|
|
|
) {
|
|
|
|
logger.warn(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ docId },
|
2020-05-06 06:09:33 -04:00
|
|
|
'track changes API is not configured, so not flushing'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2022-08-05 06:08:04 -04:00
|
|
|
if (Settings.disableTrackChanges) {
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ docId }, 'track changes is disabled, so not flushing')
|
2022-08-05 06:08:04 -04:00
|
|
|
return
|
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
return RedisManager.getHistoryType(
|
2023-03-21 08:06:13 -04:00
|
|
|
docId,
|
2021-07-13 07:04:42 -04:00
|
|
|
function (err, projectHistoryType) {
|
|
|
|
if (err != null) {
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.warn({ err, docId }, 'error getting history type')
|
2021-07-13 07:04:42 -04:00
|
|
|
}
|
|
|
|
// if there's an error continue and flush to track-changes for safety
|
|
|
|
if (
|
|
|
|
Settings.disableDoubleFlush &&
|
|
|
|
projectHistoryType === 'project-history'
|
|
|
|
) {
|
|
|
|
return logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ docId, projectHistoryType },
|
2021-07-13 07:04:42 -04:00
|
|
|
'skipping track-changes flush'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
metrics.inc('history-flush', 1, { status: 'track-changes' })
|
2023-03-21 08:06:13 -04:00
|
|
|
const url = `${Settings.apis.trackchanges.url}/project/${projectId}/doc/${docId}/flush`
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId, docId, url, projectHistoryType },
|
2021-07-13 07:04:42 -04:00
|
|
|
'flushing doc in track changes api'
|
|
|
|
)
|
|
|
|
return request.post(url, function (error, res, body) {
|
|
|
|
if (error != null) {
|
|
|
|
return logger.error(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ error, docId, projectId },
|
2021-07-13 07:04:42 -04:00
|
|
|
'track changes doc to track changes api'
|
|
|
|
)
|
|
|
|
} else if (res.statusCode < 200 && res.statusCode >= 300) {
|
|
|
|
return logger.error(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ docId, projectId },
|
2021-07-13 07:04:42 -04:00
|
|
|
`track changes api returned a failure status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2017-10-05 08:45:29 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
// flush changes in the background
|
2023-03-21 08:06:13 -04:00
|
|
|
flushProjectChangesAsync(projectId) {
|
2022-07-28 04:23:50 -04:00
|
|
|
if (!Settings.apis?.project_history?.enabled) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return HistoryManager.flushProjectChanges(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
2020-05-06 06:09:33 -04:00
|
|
|
{ background: true },
|
|
|
|
function () {}
|
|
|
|
)
|
|
|
|
},
|
2016-11-28 05:14:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
// flush changes and callback (for when we need to know the queue is flushed)
|
2023-03-21 08:06:13 -04:00
|
|
|
flushProjectChanges(projectId, options, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2022-07-28 04:23:50 -04:00
|
|
|
if (!Settings.apis?.project_history?.enabled) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
if (options.skip_history_flush) {
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ projectId }, 'skipping flush of project history')
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
metrics.inc('history-flush', 1, { status: 'project-history' })
|
2023-03-21 08:06:13 -04:00
|
|
|
const url = `${Settings.apis.project_history.url}/project/${projectId}/flush`
|
2020-05-06 06:09:33 -04:00
|
|
|
const qs = {}
|
|
|
|
if (options.background) {
|
|
|
|
qs.background = true
|
|
|
|
} // pass on the background flush option if present
|
2023-03-21 08:06:13 -04:00
|
|
|
logger.debug({ projectId, url, qs }, 'flushing doc in project history api')
|
2020-05-06 06:09:33 -04:00
|
|
|
return request.post({ url, qs }, function (error, res, body) {
|
|
|
|
if (error != null) {
|
|
|
|
logger.error(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ error, projectId },
|
2020-05-06 06:09:33 -04:00
|
|
|
'project history doc to track changes api'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
} else if (res.statusCode < 200 && res.statusCode >= 300) {
|
|
|
|
logger.error(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId },
|
2020-05-06 06:09:33 -04:00
|
|
|
`project history api returned a failure status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
} else {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2016-11-28 05:14:42 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
FLUSH_DOC_EVERY_N_OPS: 100,
|
|
|
|
FLUSH_PROJECT_EVERY_N_OPS: 500,
|
2017-11-13 06:53:39 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
recordAndFlushHistoryOps(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
ops,
|
2023-03-21 08:06:13 -04:00
|
|
|
docOpsLength,
|
|
|
|
projectOpsLength,
|
2020-05-06 06:09:33 -04:00
|
|
|
callback
|
|
|
|
) {
|
|
|
|
if (ops == null) {
|
|
|
|
ops = []
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
if (ops.length === 0) {
|
|
|
|
return callback()
|
|
|
|
}
|
2017-10-05 08:45:29 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
// record updates for project history
|
2022-07-28 04:23:50 -04:00
|
|
|
if (Settings.apis?.project_history?.enabled) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (
|
|
|
|
HistoryManager.shouldFlushHistoryOps(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectOpsLength,
|
2020-05-06 06:09:33 -04:00
|
|
|
ops.length,
|
|
|
|
HistoryManager.FLUSH_PROJECT_EVERY_N_OPS
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Do this in the background since it uses HTTP and so may be too
|
|
|
|
// slow to wait for when processing a doc update.
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectOpsLength, projectId },
|
2020-05-06 06:09:33 -04:00
|
|
|
'flushing project history api'
|
|
|
|
)
|
2023-03-21 08:06:13 -04:00
|
|
|
HistoryManager.flushProjectChangesAsync(projectId)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 08:45:29 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
// if the doc_ops_length is undefined it means the project is not using track-changes
|
|
|
|
// so we can bail out here
|
2023-03-21 08:06:13 -04:00
|
|
|
if (Settings.disableTrackChanges || typeof docOpsLength === 'undefined') {
|
2020-05-06 06:09:33 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ projectId, docId },
|
2020-05-06 06:09:33 -04:00
|
|
|
'skipping flush to track-changes, only using project-history'
|
|
|
|
)
|
|
|
|
return callback()
|
|
|
|
}
|
2019-11-15 11:53:16 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
// record updates for track-changes
|
|
|
|
return HistoryRedisManager.recordDocHasHistoryOps(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
ops,
|
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
HistoryManager.shouldFlushHistoryOps(
|
2023-03-21 08:06:13 -04:00
|
|
|
docOpsLength,
|
2020-05-06 06:09:33 -04:00
|
|
|
ops.length,
|
|
|
|
HistoryManager.FLUSH_DOC_EVERY_N_OPS
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Do this in the background since it uses HTTP and so may be too
|
|
|
|
// slow to wait for when processing a doc update.
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug(
|
2023-03-21 08:06:13 -04:00
|
|
|
{ docOpsLength, docId, projectId },
|
2020-05-06 06:09:33 -04:00
|
|
|
'flushing track changes api'
|
|
|
|
)
|
2023-03-21 08:06:13 -04:00
|
|
|
HistoryManager.flushDocChangesAsync(projectId, docId)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2017-10-05 08:45:29 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
shouldFlushHistoryOps(length, opsLength, threshold) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (!length) {
|
|
|
|
return false
|
|
|
|
} // don't flush unless we know the length
|
|
|
|
// We want to flush every 100 ops, i.e. 100, 200, 300, etc
|
|
|
|
// Find out which 'block' (i.e. 0-99, 100-199) we were in before and after pushing these
|
|
|
|
// ops. If we've changed, then we've gone over a multiple of 100 and should flush.
|
|
|
|
// (Most of the time, we will only hit 100 and then flushing will put us back to 0)
|
2023-03-21 08:06:13 -04:00
|
|
|
const previousLength = length - opsLength
|
2020-05-06 06:09:33 -04:00
|
|
|
const prevBlock = Math.floor(previousLength / threshold)
|
|
|
|
const newBlock = Math.floor(length / threshold)
|
|
|
|
return newBlock !== prevBlock
|
|
|
|
},
|
2018-02-28 12:31:43 -05:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
MAX_PARALLEL_REQUESTS: 4,
|
2018-08-16 06:13:11 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
resyncProjectHistory(projectId, projectHistoryId, docs, files, callback) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return ProjectHistoryRedisManager.queueResyncProjectStructure(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
2020-05-06 06:09:33 -04:00
|
|
|
projectHistoryId,
|
|
|
|
docs,
|
|
|
|
files,
|
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const DocumentManager = require('./DocumentManager')
|
2021-11-22 09:15:36 -05:00
|
|
|
const resyncDoc = (doc, cb) => {
|
|
|
|
DocumentManager.resyncDocContentsWithLock(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
2021-11-22 09:15:36 -05:00
|
|
|
doc.doc,
|
|
|
|
doc.path,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return async.eachLimit(
|
|
|
|
docs,
|
|
|
|
HistoryManager.MAX_PARALLEL_REQUESTS,
|
|
|
|
resyncDoc,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|