overleaf/services/document-updater/app/js/HttpController.js

406 lines
11 KiB
JavaScript
Raw Normal View History

let HttpController
const DocumentManager = require('./DocumentManager')
const HistoryManager = require('./HistoryManager')
const ProjectManager = require('./ProjectManager')
const Errors = require('./Errors')
const logger = require('logger-sharelatex')
const Metrics = require('./Metrics')
const ProjectFlusher = require('./ProjectFlusher')
const DeleteQueueManager = require('./DeleteQueueManager')
const async = require('async')
const TWO_MEGABYTES = 2 * 1024 * 1024
module.exports = HttpController = {
getDoc(req, res, next) {
let fromVersion
2020-05-11 11:07:15 -04:00
const docId = req.params.doc_id
const projectId = req.params.project_id
logger.log({ projectId, docId }, 'getting doc via http')
const timer = new Metrics.Timer('http.getDoc')
2020-05-11 10:52:06 -04:00
if (req.query.fromVersion != null) {
fromVersion = parseInt(req.query.fromVersion, 10)
} else {
fromVersion = -1
}
2020-05-11 10:45:39 -04:00
DocumentManager.getDocAndRecentOpsWithLock(
2020-05-11 11:07:15 -04:00
projectId,
docId,
fromVersion,
function (error, lines, version, ops, ranges, pathname) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId, docId }, 'got doc via http')
if (lines == null || version == null) {
return next(new Errors.NotFoundError('document not found'))
}
2020-05-11 10:45:39 -04:00
res.json({
2020-05-11 11:07:15 -04:00
id: docId,
lines,
version,
ops,
ranges,
pathname
})
}
)
},
_getTotalSizeOfLines(lines) {
let size = 0
for (const line of lines) {
size += line.length + 1
}
return size
},
getProjectDocsAndFlushIfOld(req, res, next) {
2020-05-11 11:07:15 -04:00
const projectId = req.params.project_id
2020-05-11 10:52:06 -04:00
const projectStateHash = req.query.state
// exclude is string of existing docs "id:version,id:version,..."
const excludeItems =
2020-05-11 10:47:27 -04:00
req.query.exclude != null ? req.query.exclude.split(',') : []
2020-05-11 11:07:15 -04:00
logger.log({ projectId, exclude: excludeItems }, 'getting docs via http')
const timer = new Metrics.Timer('http.getAllDocs')
const excludeVersions = {}
for (const item of excludeItems) {
const [id, version] = item.split(':')
excludeVersions[id] = version
}
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, projectStateHash, excludeVersions },
'excluding versions'
)
2020-05-11 10:45:39 -04:00
ProjectManager.getProjectDocsAndFlushIfOld(
2020-05-11 11:07:15 -04:00
projectId,
projectStateHash,
excludeVersions,
function (error, result) {
timer.done()
if (error instanceof Errors.ProjectStateChangedError) {
2020-05-11 10:45:39 -04:00
res.sendStatus(409) // conflict
2020-05-11 10:52:06 -04:00
} else if (error) {
2020-05-11 10:45:39 -04:00
next(error)
} else {
logger.log(
{
2020-05-11 11:07:15 -04:00
projectId,
result: result.map((doc) => `${doc._id}:${doc.v}`)
},
'got docs via http'
)
2020-05-11 10:45:39 -04:00
res.send(result)
}
}
)
},
clearProjectState(req, res, next) {
2020-05-11 11:07:15 -04:00
const projectId = req.params.project_id
const timer = new Metrics.Timer('http.clearProjectState')
2020-05-11 11:07:15 -04:00
logger.log({ projectId }, 'clearing project state via http')
ProjectManager.clearProjectState(projectId, function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
2020-05-11 10:45:39 -04:00
next(error)
} else {
2020-05-11 10:45:39 -04:00
res.sendStatus(200)
}
})
},
setDoc(req, res, next) {
2020-05-11 11:07:15 -04:00
const docId = req.params.doc_id
const projectId = req.params.project_id
const { lines, source, user_id: userId, undoing } = req.body
const lineSize = HttpController._getTotalSizeOfLines(lines)
if (lineSize > TWO_MEGABYTES) {
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, docId, source, lineSize, userId },
'document too large, returning 406 response'
)
return res.sendStatus(406)
}
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, docId, lines, source, userId, undoing },
'setting doc via http'
)
const timer = new Metrics.Timer('http.setDoc')
2020-05-11 10:45:39 -04:00
DocumentManager.setDocWithLock(
2020-05-11 11:07:15 -04:00
projectId,
docId,
lines,
source,
2020-05-11 11:07:15 -04:00
userId,
undoing,
function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId, docId }, 'set doc via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
}, // No Content
flushDocIfLoaded(req, res, next) {
2020-05-11 11:07:15 -04:00
const docId = req.params.doc_id
const projectId = req.params.project_id
logger.log({ projectId, docId }, 'flushing doc via http')
const timer = new Metrics.Timer('http.flushDoc')
2020-05-11 11:07:15 -04:00
DocumentManager.flushDocIfLoadedWithLock(projectId, docId, function (
2020-05-11 10:45:39 -04:00
error
) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
2020-05-11 10:45:39 -04:00
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId, docId }, 'flushed doc via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
})
}, // No Content
deleteDoc(req, res, next) {
2020-05-11 11:07:15 -04:00
const docId = req.params.doc_id
const projectId = req.params.project_id
const ignoreFlushErrors = req.query.ignore_flush_errors === 'true'
const timer = new Metrics.Timer('http.deleteDoc')
2020-05-11 11:07:15 -04:00
logger.log({ projectId, docId }, 'deleting doc via http')
2020-05-11 10:45:39 -04:00
DocumentManager.flushAndDeleteDocWithLock(
2020-05-11 11:07:15 -04:00
projectId,
docId,
{ ignoreFlushErrors },
function (error) {
timer.done()
// There is no harm in flushing project history if the previous call
// failed and sometimes it is required
2020-05-11 11:07:15 -04:00
HistoryManager.flushProjectChangesAsync(projectId)
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId, docId }, 'deleted doc via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
}, // No Content
flushProject(req, res, next) {
2020-05-11 11:07:15 -04:00
const projectId = req.params.project_id
logger.log({ projectId }, 'flushing project via http')
const timer = new Metrics.Timer('http.flushProject')
2020-05-11 11:07:15 -04:00
ProjectManager.flushProjectWithLocks(projectId, function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId }, 'flushed project via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
})
}, // No Content
deleteProject(req, res, next) {
2020-05-11 11:07:15 -04:00
const projectId = req.params.project_id
logger.log({ projectId }, 'deleting project via http')
const options = {}
2020-05-11 10:52:06 -04:00
if (req.query.background) {
options.background = true
} // allow non-urgent flushes to be queued
2020-05-11 10:52:06 -04:00
if (req.query.shutdown) {
options.skip_history_flush = true
} // don't flush history when realtime shuts down
2020-05-11 10:52:06 -04:00
if (req.query.background) {
2020-05-11 11:07:15 -04:00
ProjectManager.queueFlushAndDeleteProject(projectId, function (error) {
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId }, 'queue delete of project via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}) // No Content
} else {
const timer = new Metrics.Timer('http.deleteProject')
2020-05-11 10:45:39 -04:00
ProjectManager.flushAndDeleteProjectWithLocks(
2020-05-11 11:07:15 -04:00
projectId,
options,
function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId }, 'deleted project via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
}
}, // No Content
deleteMultipleProjects(req, res, next) {
2020-05-11 11:07:15 -04:00
const projectIds = req.body.project_ids || []
logger.log({ projectIds }, 'deleting multiple projects via http')
2020-05-11 10:45:39 -04:00
async.eachSeries(
2020-05-11 11:07:15 -04:00
projectIds,
function (projectId, cb) {
logger.log({ projectId }, 'queue delete of project via http')
ProjectManager.queueFlushAndDeleteProject(projectId, cb)
},
function (error) {
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
}, // No Content
acceptChanges(req, res, next) {
2020-05-11 11:07:15 -04:00
const { project_id: projectId, doc_id: docId } = req.params
let changeIds = req.body.change_ids
if (changeIds == null) {
changeIds = [req.params.change_id]
}
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, docId },
`accepting ${changeIds.length} changes via http`
)
const timer = new Metrics.Timer('http.acceptChanges')
2020-05-11 10:45:39 -04:00
DocumentManager.acceptChangesWithLock(
2020-05-11 11:07:15 -04:00
projectId,
docId,
changeIds,
function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, docId },
`accepted ${changeIds.length} changes via http`
)
2020-05-11 11:07:15 -04:00
res.sendStatus(204) // No Content
}
)
2020-05-11 11:07:15 -04:00
},
deleteComment(req, res, next) {
2020-05-11 11:07:15 -04:00
const {
project_id: projectId,
doc_id: docId,
comment_id: commentId
} = req.params
logger.log({ projectId, docId, commentId }, 'deleting comment via http')
const timer = new Metrics.Timer('http.deleteComment')
2020-05-11 10:45:39 -04:00
DocumentManager.deleteCommentWithLock(
2020-05-11 11:07:15 -04:00
projectId,
docId,
commentId,
function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId, docId, commentId }, 'deleted comment via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
}, // No Content
updateProject(req, res, next) {
const timer = new Metrics.Timer('http.updateProject')
2020-05-11 11:07:15 -04:00
const projectId = req.params.project_id
const {
projectHistoryId,
userId,
docUpdates,
fileUpdates,
version
} = req.body
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, docUpdates, fileUpdates, version },
'updating project via http'
)
2020-05-11 10:45:39 -04:00
ProjectManager.updateProjectWithLocks(
2020-05-11 11:07:15 -04:00
projectId,
projectHistoryId,
userId,
docUpdates,
fileUpdates,
version,
function (error) {
timer.done()
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId }, 'updated project via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
}, // No Content
resyncProjectHistory(req, res, next) {
2020-05-11 11:07:15 -04:00
const projectId = req.params.project_id
const { projectHistoryId, docs, files } = req.body
logger.log(
2020-05-11 11:07:15 -04:00
{ projectId, docs, files },
'queuing project history resync via http'
)
2020-05-11 10:45:39 -04:00
HistoryManager.resyncProjectHistory(
2020-05-11 11:07:15 -04:00
projectId,
projectHistoryId,
docs,
files,
function (error) {
2020-05-11 10:52:06 -04:00
if (error) {
return next(error)
}
2020-05-11 11:07:15 -04:00
logger.log({ projectId }, 'queued project history resync via http')
2020-05-11 10:45:39 -04:00
res.sendStatus(204)
}
)
},
flushAllProjects(req, res, next) {
res.setTimeout(5 * 60 * 1000)
const options = {
limit: req.query.limit || 1000,
concurrency: req.query.concurrency || 5,
dryRun: req.query.dryRun || false
}
2020-05-11 11:07:15 -04:00
ProjectFlusher.flushAllProjects(options, function (err, projectIds) {
2020-05-11 10:52:06 -04:00
if (err) {
logger.err({ err }, 'error bulk flushing projects')
2020-05-11 10:45:39 -04:00
res.sendStatus(500)
} else {
2020-05-11 11:07:15 -04:00
res.send(projectIds)
}
})
},
flushQueuedProjects(req, res, next) {
res.setTimeout(10 * 60 * 1000)
const options = {
limit: req.query.limit || 1000,
timeout: 5 * 60 * 1000,
min_delete_age: req.query.min_delete_age || 5 * 60 * 1000
}
2020-05-11 10:45:39 -04:00
DeleteQueueManager.flushAndDeleteOldProjects(options, function (
err,
flushed
) {
2020-05-11 10:52:06 -04:00
if (err) {
logger.err({ err }, 'error flushing old projects')
2020-05-11 10:45:39 -04:00
res.sendStatus(500)
} else {
logger.log({ flushed }, 'flush of queued projects completed')
2020-05-11 10:45:39 -04:00
res.send({ flushed })
}
})
}
}