2014-02-12 05:40:42 -05:00
|
|
|
RedisManager = require "./RedisManager"
|
|
|
|
DocumentManager = require "./DocumentManager"
|
2018-01-23 06:11:15 -05:00
|
|
|
HistoryManager = require "./HistoryManager"
|
2014-02-12 05:40:42 -05:00
|
|
|
async = require "async"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
Metrics = require "./Metrics"
|
2017-08-07 09:43:28 -04:00
|
|
|
Errors = require "./Errors"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
module.exports = ProjectManager =
|
|
|
|
flushProjectWithLocks: (project_id, _callback = (error) ->) ->
|
|
|
|
timer = new Metrics.Timer("projectManager.flushProjectWithLocks")
|
|
|
|
callback = (args...) ->
|
|
|
|
timer.done()
|
|
|
|
_callback(args...)
|
|
|
|
|
|
|
|
RedisManager.getDocIdsInProject project_id, (error, doc_ids) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
jobs = []
|
|
|
|
errors = []
|
|
|
|
for doc_id in (doc_ids or [])
|
|
|
|
do (doc_id) ->
|
|
|
|
jobs.push (callback) ->
|
|
|
|
DocumentManager.flushDocIfLoadedWithLock project_id, doc_id, (error) ->
|
2017-12-19 07:27:57 -05:00
|
|
|
if error? and error instanceof Errors.NotFoundError
|
2017-12-20 08:26:57 -05:00
|
|
|
logger.warn err: error, project_id: project_id, doc_id: doc_id, "found deleted doc when flushing"
|
2017-12-19 07:27:57 -05:00
|
|
|
callback()
|
|
|
|
else if error?
|
2014-02-12 05:40:42 -05:00
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "error flushing doc"
|
|
|
|
errors.push(error)
|
2017-12-19 07:27:57 -05:00
|
|
|
callback()
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
logger.log project_id: project_id, doc_ids: doc_ids, "flushing docs"
|
|
|
|
async.series jobs, () ->
|
|
|
|
if errors.length > 0
|
|
|
|
callback new Error("Errors flushing docs. See log for details")
|
|
|
|
else
|
|
|
|
callback(null)
|
|
|
|
|
|
|
|
flushAndDeleteProjectWithLocks: (project_id, _callback = (error) ->) ->
|
|
|
|
timer = new Metrics.Timer("projectManager.flushAndDeleteProjectWithLocks")
|
|
|
|
callback = (args...) ->
|
|
|
|
timer.done()
|
|
|
|
_callback(args...)
|
|
|
|
|
|
|
|
RedisManager.getDocIdsInProject project_id, (error, doc_ids) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
jobs = []
|
|
|
|
errors = []
|
|
|
|
for doc_id in (doc_ids or [])
|
|
|
|
do (doc_id) ->
|
|
|
|
jobs.push (callback) ->
|
|
|
|
DocumentManager.flushAndDeleteDocWithLock project_id, doc_id, (error) ->
|
|
|
|
if error?
|
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "error deleting doc"
|
|
|
|
errors.push(error)
|
|
|
|
callback()
|
|
|
|
|
|
|
|
logger.log project_id: project_id, doc_ids: doc_ids, "deleting docs"
|
|
|
|
async.series jobs, () ->
|
|
|
|
if errors.length > 0
|
|
|
|
callback new Error("Errors deleting docs. See log for details")
|
|
|
|
else
|
|
|
|
callback(null)
|
2017-07-28 11:34:32 -04:00
|
|
|
|
2017-10-11 10:29:57 -04:00
|
|
|
getProjectDocsAndFlushIfOld: (project_id, projectStateHash, excludeVersions = {}, _callback = (error, docs) ->) ->
|
|
|
|
timer = new Metrics.Timer("projectManager.getProjectDocsAndFlushIfOld")
|
2017-07-28 11:34:32 -04:00
|
|
|
callback = (args...) ->
|
|
|
|
timer.done()
|
|
|
|
_callback(args...)
|
|
|
|
|
2017-08-07 09:43:28 -04:00
|
|
|
RedisManager.checkOrSetProjectState project_id, projectStateHash, (error, projectStateChanged) ->
|
2017-09-07 09:12:48 -04:00
|
|
|
if error?
|
2017-10-11 10:29:57 -04:00
|
|
|
logger.error err: error, project_id: project_id, "error getting/setting project state in getProjectDocsAndFlushIfOld"
|
2017-09-07 09:12:48 -04:00
|
|
|
return callback(error)
|
2017-08-07 09:43:28 -04:00
|
|
|
# we can't return docs if project structure has changed
|
2017-10-06 07:23:23 -04:00
|
|
|
if projectStateChanged
|
|
|
|
return callback Errors.ProjectStateChangedError("project state changed")
|
2017-08-07 09:43:28 -04:00
|
|
|
# project structure hasn't changed, return doc content from redis
|
|
|
|
RedisManager.getDocIdsInProject project_id, (error, doc_ids) ->
|
2017-09-07 09:12:48 -04:00
|
|
|
if error?
|
|
|
|
logger.error err: error, project_id: project_id, "error getting doc ids in getProjectDocs"
|
|
|
|
return callback(error)
|
2017-08-07 09:43:28 -04:00
|
|
|
jobs = []
|
|
|
|
for doc_id in doc_ids or []
|
|
|
|
do (doc_id) ->
|
|
|
|
jobs.push (cb) ->
|
2017-10-06 07:23:23 -04:00
|
|
|
# get the doc lines from redis
|
|
|
|
DocumentManager.getDocAndFlushIfOldWithLock project_id, doc_id, (err, lines, version) ->
|
|
|
|
if err?
|
2017-10-11 10:29:57 -04:00
|
|
|
logger.error err:err, project_id: project_id, doc_id: doc_id, "error getting project doc lines in getProjectDocsAndFlushIfOld"
|
2017-10-06 07:23:23 -04:00
|
|
|
return cb(err)
|
|
|
|
doc = {_id:doc_id, lines:lines, v:version} # create a doc object to return
|
|
|
|
cb(null, doc)
|
|
|
|
async.series jobs, (error, docs) ->
|
2017-08-07 09:43:28 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, docs)
|
2017-09-08 10:50:26 -04:00
|
|
|
|
|
|
|
clearProjectState: (project_id, callback = (error) ->) ->
|
|
|
|
RedisManager.clearProjectState project_id, callback
|
2017-11-01 15:16:49 -04:00
|
|
|
|
2017-11-06 11:14:27 -05:00
|
|
|
updateProjectWithLocks: (project_id, user_id, docUpdates, fileUpdates, _callback = (error) ->) ->
|
2017-11-01 15:16:49 -04:00
|
|
|
timer = new Metrics.Timer("projectManager.updateProject")
|
|
|
|
callback = (args...) ->
|
|
|
|
timer.done()
|
|
|
|
_callback(args...)
|
|
|
|
|
2017-11-06 11:14:27 -05:00
|
|
|
handleDocUpdate = (update, cb) ->
|
2017-11-01 15:16:49 -04:00
|
|
|
doc_id = update.id
|
2017-11-10 09:54:56 -05:00
|
|
|
if update.docLines?
|
|
|
|
RedisManager.addEntity project_id, 'doc', doc_id, user_id, update, cb
|
|
|
|
else
|
|
|
|
DocumentManager.renameDocWithLock project_id, doc_id, user_id, update, cb
|
2017-11-01 15:16:49 -04:00
|
|
|
|
2017-11-06 11:14:27 -05:00
|
|
|
handleFileUpdate = (update, cb) ->
|
|
|
|
file_id = update.id
|
2017-11-10 09:54:56 -05:00
|
|
|
if update.url?
|
|
|
|
RedisManager.addEntity project_id, 'file', file_id, user_id, update, cb
|
|
|
|
else
|
|
|
|
RedisManager.renameFile project_id, file_id, user_id, update, cb
|
2017-11-06 11:14:27 -05:00
|
|
|
|
|
|
|
async.each docUpdates, handleDocUpdate, (error) ->
|
|
|
|
return callback(error) if error?
|
2018-01-23 06:11:15 -05:00
|
|
|
async.each fileUpdates, handleFileUpdate, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
RedisManager.numQueuedProjectUpdates project_id, (error, length) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if length >= HistoryManager.FLUSH_PROJECT_EVERY_N_OPS
|
|
|
|
HistoryManager.flushProjectChanges project_id, length
|
|
|
|
callback()
|