2020-05-06 06:09:33 -04:00
|
|
|
const RedisManager = require('./RedisManager')
|
|
|
|
const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
|
|
|
|
const DocumentManager = require('./DocumentManager')
|
|
|
|
const HistoryManager = require('./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 Metrics = require('./Metrics')
|
|
|
|
const Errors = require('./Errors')
|
|
|
|
|
2020-05-14 16:54:08 -04:00
|
|
|
module.exports = {
|
2020-05-14 17:09:01 -04:00
|
|
|
flushProjectWithLocks,
|
|
|
|
flushAndDeleteProjectWithLocks,
|
|
|
|
queueFlushAndDeleteProject,
|
|
|
|
getProjectDocsTimestamps,
|
|
|
|
getProjectDocsAndFlushIfOld,
|
|
|
|
clearProjectState,
|
2021-07-13 07:04:42 -04:00
|
|
|
updateProjectWithLocks,
|
2020-05-14 17:09:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function flushProjectWithLocks(projectId, _callback) {
|
|
|
|
const timer = new Metrics.Timer('projectManager.flushProjectWithLocks')
|
|
|
|
const callback = function (...args) {
|
|
|
|
timer.done()
|
|
|
|
_callback(...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
const errors = []
|
2021-07-13 07:04:42 -04:00
|
|
|
const jobs = docIds.map(docId => callback => {
|
|
|
|
DocumentManager.flushDocIfLoadedWithLock(projectId, docId, error => {
|
2020-05-14 17:09:01 -04:00
|
|
|
if (error instanceof Errors.NotFoundError) {
|
|
|
|
logger.warn(
|
|
|
|
{ err: error, projectId, docId },
|
|
|
|
'found deleted doc when flushing'
|
|
|
|
)
|
|
|
|
callback()
|
|
|
|
} else if (error) {
|
|
|
|
logger.error({ err: error, projectId, docId }, 'error flushing doc')
|
|
|
|
errors.push(error)
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug({ projectId, docIds }, 'flushing docs')
|
2020-05-14 17:09:01 -04:00
|
|
|
async.series(jobs, () => {
|
|
|
|
if (errors.length > 0) {
|
|
|
|
callback(new Error('Errors flushing docs. See log for details'))
|
|
|
|
} else {
|
|
|
|
callback(null)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function flushAndDeleteProjectWithLocks(projectId, options, _callback) {
|
|
|
|
const timer = new Metrics.Timer(
|
|
|
|
'projectManager.flushAndDeleteProjectWithLocks'
|
|
|
|
)
|
|
|
|
const callback = function (...args) {
|
|
|
|
timer.done()
|
|
|
|
_callback(...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const errors = []
|
2021-07-13 07:04:42 -04:00
|
|
|
const jobs = docIds.map(docId => callback => {
|
|
|
|
DocumentManager.flushAndDeleteDocWithLock(projectId, docId, {}, error => {
|
|
|
|
if (error) {
|
|
|
|
logger.error({ err: error, projectId, docId }, 'error deleting doc')
|
|
|
|
errors.push(error)
|
2020-05-14 17:09:01 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
callback()
|
|
|
|
})
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2021-09-30 04:28:32 -04:00
|
|
|
logger.debug({ projectId, docIds }, 'deleting docs')
|
2020-05-14 17:09:01 -04:00
|
|
|
async.series(jobs, () =>
|
|
|
|
// When deleting the project here we want to ensure that project
|
|
|
|
// history is completely flushed because the project may be
|
|
|
|
// deleted in web after this call completes, and so further
|
|
|
|
// attempts to flush would fail after that.
|
2021-07-13 07:04:42 -04:00
|
|
|
HistoryManager.flushProjectChanges(projectId, options, error => {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (errors.length > 0) {
|
2020-05-14 17:09:01 -04:00
|
|
|
callback(new Error('Errors deleting docs. See log for details'))
|
|
|
|
} else if (error) {
|
|
|
|
callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
} else {
|
2020-05-14 16:35:10 -04:00
|
|
|
callback(null)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function queueFlushAndDeleteProject(projectId, callback) {
|
2021-07-13 07:04:42 -04:00
|
|
|
RedisManager.queueFlushAndDeleteProject(projectId, error => {
|
2020-05-14 17:09:01 -04:00
|
|
|
if (error) {
|
|
|
|
logger.error(
|
|
|
|
{ projectId, error },
|
|
|
|
'error adding project to flush and delete queue'
|
|
|
|
)
|
|
|
|
return callback(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
Metrics.inc('queued-delete')
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-14 17:09:01 -04:00
|
|
|
function getProjectDocsTimestamps(projectId, callback) {
|
|
|
|
RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (docIds.length === 0) {
|
|
|
|
return callback(null, [])
|
|
|
|
}
|
|
|
|
RedisManager.getDocTimestamps(docIds, (error, timestamps) => {
|
2020-05-14 16:53:22 -04:00
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
callback(null, timestamps)
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-14 17:09:01 -04:00
|
|
|
function getProjectDocsAndFlushIfOld(
|
|
|
|
projectId,
|
|
|
|
projectStateHash,
|
|
|
|
excludeVersions,
|
|
|
|
_callback
|
|
|
|
) {
|
|
|
|
const timer = new Metrics.Timer('projectManager.getProjectDocsAndFlushIfOld')
|
|
|
|
const callback = function (...args) {
|
|
|
|
timer.done()
|
|
|
|
_callback(...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisManager.checkOrSetProjectState(
|
|
|
|
projectId,
|
|
|
|
projectStateHash,
|
|
|
|
(error, projectStateChanged) => {
|
2020-05-14 16:53:22 -04:00
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
logger.error(
|
2020-05-14 17:09:01 -04:00
|
|
|
{ err: error, projectId },
|
|
|
|
'error getting/setting project state in getProjectDocsAndFlushIfOld'
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
// we can't return docs if project structure has changed
|
|
|
|
if (projectStateChanged) {
|
|
|
|
return callback(
|
|
|
|
Errors.ProjectStateChangedError('project state changed')
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
// project structure hasn't changed, return doc content from redis
|
|
|
|
RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
|
2020-05-14 16:53:22 -04:00
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
logger.error(
|
2020-05-14 16:58:57 -04:00
|
|
|
{ err: error, projectId },
|
2020-05-14 17:09:01 -04:00
|
|
|
'error getting doc ids in getProjectDocs'
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
// get the doc lines from redis
|
2021-07-13 07:04:42 -04:00
|
|
|
const jobs = docIds.map(docId => cb => {
|
2020-05-14 17:09:01 -04:00
|
|
|
DocumentManager.getDocAndFlushIfOldWithLock(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
(err, lines, version) => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(
|
|
|
|
{ err, projectId, docId },
|
|
|
|
'error getting project doc lines in getProjectDocsAndFlushIfOld'
|
|
|
|
)
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
const doc = { _id: docId, lines, v: version } // create a doc object to return
|
|
|
|
cb(null, doc)
|
|
|
|
}
|
2020-05-14 17:03:14 -04:00
|
|
|
)
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
|
|
|
async.series(jobs, (error, docs) => {
|
2020-05-14 16:53:22 -04:00
|
|
|
if (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
callback(null, docs)
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-14 17:09:01 -04:00
|
|
|
function clearProjectState(projectId, callback) {
|
|
|
|
RedisManager.clearProjectState(projectId, callback)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-14 17:09:01 -04:00
|
|
|
function updateProjectWithLocks(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
userId,
|
2020-05-15 15:54:31 -04:00
|
|
|
updates,
|
|
|
|
projectVersion,
|
2020-05-14 17:09:01 -04:00
|
|
|
_callback
|
|
|
|
) {
|
|
|
|
const timer = new Metrics.Timer('projectManager.updateProject')
|
|
|
|
const callback = function (...args) {
|
|
|
|
timer.done()
|
|
|
|
_callback(...args)
|
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-14 17:09:01 -04:00
|
|
|
let projectSubversion = 0 // project versions can have multiple operations
|
|
|
|
let projectOpsLength = 0
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2020-05-15 15:54:31 -04:00
|
|
|
function handleUpdate(update, cb) {
|
|
|
|
update.version = `${projectVersion}.${projectSubversion++}`
|
|
|
|
switch (update.type) {
|
|
|
|
case 'add-doc':
|
|
|
|
ProjectHistoryRedisManager.queueAddEntity(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
update.id,
|
|
|
|
userId,
|
|
|
|
update,
|
|
|
|
(error, count) => {
|
|
|
|
projectOpsLength = count
|
|
|
|
cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
break
|
|
|
|
case 'rename-doc':
|
2021-11-22 05:08:26 -05:00
|
|
|
if (!update.newPathname) {
|
|
|
|
// an empty newPathname signifies a delete, so there is no need to
|
|
|
|
// update the pathname in redis
|
|
|
|
ProjectHistoryRedisManager.queueRenameEntity(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
update.id,
|
|
|
|
userId,
|
|
|
|
update,
|
|
|
|
(error, count) => {
|
|
|
|
projectOpsLength = count
|
|
|
|
cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// rename the doc in redis before queuing the update
|
|
|
|
DocumentManager.renameDocWithLock(
|
|
|
|
projectId,
|
|
|
|
update.id,
|
|
|
|
userId,
|
|
|
|
update,
|
|
|
|
projectHistoryId,
|
|
|
|
error => {
|
|
|
|
if (error) {
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
ProjectHistoryRedisManager.queueRenameEntity(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
update.id,
|
|
|
|
userId,
|
|
|
|
update,
|
|
|
|
(error, count) => {
|
|
|
|
projectOpsLength = count
|
|
|
|
cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-15 15:54:31 -04:00
|
|
|
break
|
|
|
|
case 'add-file':
|
|
|
|
ProjectHistoryRedisManager.queueAddEntity(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
'file',
|
|
|
|
update.id,
|
|
|
|
userId,
|
|
|
|
update,
|
|
|
|
(error, count) => {
|
|
|
|
projectOpsLength = count
|
|
|
|
cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
break
|
|
|
|
case 'rename-file':
|
|
|
|
ProjectHistoryRedisManager.queueRenameEntity(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
'file',
|
|
|
|
update.id,
|
|
|
|
userId,
|
|
|
|
update,
|
|
|
|
(error, count) => {
|
|
|
|
projectOpsLength = count
|
|
|
|
cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
cb(new Error(`Unknown update type: ${update.type}`))
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-14 17:09:01 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
|
2021-07-13 07:04:42 -04:00
|
|
|
async.eachSeries(updates, handleUpdate, error => {
|
2020-05-14 17:09:01 -04:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-05-15 15:54:31 -04:00
|
|
|
if (
|
|
|
|
HistoryManager.shouldFlushHistoryOps(
|
|
|
|
projectOpsLength,
|
|
|
|
updates.length,
|
|
|
|
HistoryManager.FLUSH_PROJECT_EVERY_N_OPS
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
HistoryManager.flushProjectChangesAsync(projectId)
|
|
|
|
}
|
|
|
|
callback()
|
2020-05-14 17:09:01 -04:00
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|