overleaf/services/track-changes/app/coffee/DocArchiveManager.coffee

62 lines
2.2 KiB
CoffeeScript
Raw Normal View History

2015-08-06 10:11:43 -04:00
MongoManager = require "./MongoManager"
2015-08-06 14:46:44 -04:00
MongoAWS = require "./MongoAWS"
LockManager = require "./LockManager"
DocstoreHandler = require "./DocstoreHandler"
2015-08-06 10:11:43 -04:00
logger = require "logger-sharelatex"
_ = require "underscore"
async = require "async"
settings = require("settings-sharelatex")
module.exports = DocArchiveManager =
archiveAllDocsChanges: (project_id, callback = (error, docs) ->) ->
DocstoreHandler.getAllDocs project_id, (error, docs) ->
2015-08-06 10:11:43 -04:00
if error?
return callback(error)
else if !docs?
return callback new Error("No docs for project #{project_id}")
jobs = _.map docs, (doc) ->
(cb)-> DocArchiveManager.archiveDocChangesWithLock project_id, doc._id, cb
2015-08-06 10:11:43 -04:00
async.series jobs, callback
archiveDocChangesWithLock: (project_id, doc_id, callback = (error) ->) ->
LockManager.runWithLock(
"HistoryArchiveLock:#{doc_id}",
(releaseLock) ->
DocArchiveManager.archiveDocChanges project_id, doc_id, releaseLock
callback
)
2015-08-06 10:11:43 -04:00
archiveDocChanges: (project_id, doc_id, callback)->
2015-08-06 16:09:36 -04:00
MongoManager.getDocChangesCount doc_id, (error, count) ->
if count == 0
2015-08-09 14:47:47 -04:00
return callback()
2015-08-06 16:09:36 -04:00
else
MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
MongoAWS.archiveDocHistory project_id, doc_id, (error) ->
2015-09-02 17:47:34 -04:00
logger.log doc_id:doc_id, error: error, "export to S3"
MongoManager.markDocHistoryAsArchived doc_id, update, (error) ->
return callback(error) if error?
callback()
2015-08-06 16:09:36 -04:00
unArchiveAllDocsChanges: (project_id, callback = (error, docs) ->) ->
DocstoreHandler.getAllDocs project_id, (error, docs) ->
2015-08-06 16:09:36 -04:00
if error?
return callback(error)
else if !docs?
return callback new Error("No docs for project #{project_id}")
jobs = _.map docs, (doc) ->
(cb)-> DocArchiveManager.unArchiveDocChanges project_id, doc._id, cb
async.series jobs, callback
unArchiveDocChanges: (project_id, doc_id, callback)->
2015-08-09 18:52:32 -04:00
MongoManager.getArchivedDocChanges doc_id, (error, count) ->
2015-08-09 14:47:47 -04:00
if count == 0
return callback()
else
MongoAWS.unArchiveDocHistory project_id, doc_id, (error) ->
2015-09-02 17:47:34 -04:00
logger.log doc_id:doc_id, error: error, "import from S3"
2015-08-09 16:50:15 -04:00
MongoManager.markDocHistoryAsUnarchived doc_id, (error) ->
return callback(error) if error?
callback()