<commit_before>MongoManager = require "./MongoManager"
MongoAWS = require "./MongoAWS"
LockManager = require "./LockManager"
DocstoreHandler = require "./DocstoreHandler"
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) ->
			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
			async.series jobs, callback

	archiveDocChangesWithLock: (project_id, doc_id, callback = (error) ->) ->
		job = (releaseLock) ->
			DocArchiveManager.archiveDocChanges project_id, doc_id, releaseLock
		LockManager.runWithLock("HistoryArchiveLock:#{doc_id}", job, callback)

	archiveDocChanges: (project_id, doc_id, callback)->
		MongoManager.getDocChangesCount doc_id, (error, count) ->
			if count == 0
				return callback()
			else
				MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
					MongoAWS.archiveDocHistory project_id, doc_id, (error) ->
						logger.log doc_id:doc_id, error: error, "export to S3"
						MongoManager.markDocHistoryAsArchived doc_id, update, (error) ->
							return callback(error) if error?
							callback()

	unArchiveAllDocsChanges: (project_id, callback = (error, docs) ->) ->
		DocstoreHandler.getAllDocs project_id, (error, docs) ->
			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)->
		MongoManager.getArchivedDocChanges doc_id, (error, count) ->
			if count == 0
				return callback()
			else
				MongoAWS.unArchiveDocHistory project_id, doc_id, (error) ->
					logger.log doc_id:doc_id, error: error, "import from S3"
					MongoManager.markDocHistoryAsUnarchived doc_id, (error) ->
						return callback(error) if error?
						callback()
<commit_msg>added some missing error handling<commit_after>MongoManager = require "./MongoManager"
MongoAWS = require "./MongoAWS"
LockManager = require "./LockManager"
DocstoreHandler = require "./DocstoreHandler"
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) ->
			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
			async.series jobs, callback

	archiveDocChangesWithLock: (project_id, doc_id, callback = (error) ->) ->
		job = (releaseLock) ->
			DocArchiveManager.archiveDocChanges project_id, doc_id, releaseLock
		LockManager.runWithLock("HistoryArchiveLock:#{doc_id}", job, callback)

	archiveDocChanges: (project_id, doc_id, callback)->
		MongoManager.getDocChangesCount doc_id, (error, count) ->
			return callback(error) if error?
			if count == 0
				return callback()
			else
				MongoManager.getLastCompressedUpdate doc_id, (error, update) ->
					return callback(error) if error?
					MongoAWS.archiveDocHistory project_id, doc_id, (error) ->
						return callback(error) if error?
						logger.log doc_id:doc_id, project_id:project_id, "exported document to S3"
						MongoManager.markDocHistoryAsArchived doc_id, update, (error) ->
							return callback(error) if error?
							callback()

	unArchiveAllDocsChanges: (project_id, callback = (error, docs) ->) ->
		DocstoreHandler.getAllDocs project_id, (error, docs) ->
			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)->
		MongoManager.getArchivedDocChanges doc_id, (error, count) ->
			return callback(error) if error?
			if count == 0
				return callback()
			else
				MongoAWS.unArchiveDocHistory project_id, doc_id, (error) ->
					return callback(error) if error?
					logger.log doc_id:doc_id, project_id:project_id, "imported document from S3"
					MongoManager.markDocHistoryAsUnarchived doc_id, (error) ->
						return callback(error) if error?
						callback()

{'fraction_non_alphanumeric': 0.11292005844291379, 'fraction_numerical': 0.0016697975370486328, 'mean_word_length': 4.318534961154273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1347146', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1476, 'n_words': 386}