Просмотр исходного кода

ensure that project history is flushed when the project is deleted

Brian Gough 8 лет назад
Родитель
Сommit
aae1352519

+ 9 - 0
services/document-updater/app/coffee/HistoryManager.coffee

@@ -20,16 +20,25 @@ module.exports = HistoryManager =
 			else if res.statusCode < 200 and res.statusCode >= 300
 				logger.error { doc_id, project_id }, "track changes api returned a failure status code: #{res.statusCode}"
 
+	# flush changes in the background
 	flushProjectChangesAsync: (project_id) ->
 		return if !Settings.apis?.project_history?.enabled
+		HistoryManager.flushProjectChanges project_id, ->
 
+	# flush changes and callback (for when we need to know the queue is flushed)
+	flushProjectChanges: (project_id, callback = (error) ->) ->
+		return callback() if !Settings.apis?.project_history?.enabled
 		url = "#{Settings.apis.project_history.url}/project/#{project_id}/flush"
 		logger.log { project_id, url }, "flushing doc in project history api"
 		request.post url, (error, res, body)->
 			if error?
 				logger.error { error, project_id}, "project history doc to track changes api"
+				return callback(error)
 			else if res.statusCode < 200 and res.statusCode >= 300
 				logger.error { project_id }, "project history api returned a failure status code: #{res.statusCode}"
+				return callback(error)
+			else
+				return callback()
 
 	FLUSH_DOC_EVERY_N_OPS: 100
 	FLUSH_PROJECT_EVERY_N_OPS: 500

+ 11 - 8
services/document-updater/app/coffee/ProjectManager.coffee

@@ -60,14 +60,17 @@ module.exports = ProjectManager =
 
 			logger.log project_id: project_id, doc_ids: doc_ids, "deleting docs"
 			async.series jobs, () ->
-				# There is no harm in flushing project history if the previous call
-				# failed and sometimes it is required
-				HistoryManager.flushProjectChangesAsync project_id
-
-				if errors.length > 0
-					callback new Error("Errors deleting docs. See log for details")
-				else
-					callback(null)
+				# 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.
+				HistoryManager.flushProjectChanges project_id, (error) ->
+					if errors.length > 0
+						callback new Error("Errors deleting docs. See log for details")
+					else if error?
+						callback(error)
+					else
+						callback(null)
 
 	getProjectDocsAndFlushIfOld: (project_id, projectStateHash, excludeVersions = {}, _callback = (error, docs) ->) ->
 		timer = new Metrics.Timer("projectManager.getProjectDocsAndFlushIfOld")

+ 5 - 5
services/document-updater/test/unit/coffee/ProjectManager/flushAndDeleteProjectTests.coffee

@@ -12,7 +12,7 @@ describe "ProjectManager - flushAndDeleteProject", ->
 			"./DocumentManager": @DocumentManager = {}
 			"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
 			"./HistoryManager": @HistoryManager =
-				flushProjectChangesAsync: sinon.stub()
+				flushProjectChanges: sinon.stub().callsArg(1)
 			"./Metrics": @Metrics =
 				Timer: class Timer
 					done: sinon.stub()
@@ -40,8 +40,8 @@ describe "ProjectManager - flushAndDeleteProject", ->
 					.should.equal true
 
 		it "should flush project history", ->
-			@HistoryManager.flushProjectChangesAsync
-				.calledWithExactly(@project_id)
+			@HistoryManager.flushProjectChanges
+				.calledWith(@project_id)
 				.should.equal true
 
 		it "should call the callback without error", ->
@@ -70,8 +70,8 @@ describe "ProjectManager - flushAndDeleteProject", ->
 					.should.equal true
 
 		it "should still flush project history", ->
-			@HistoryManager.flushProjectChangesAsync
-				.calledWithExactly(@project_id)
+			@HistoryManager.flushProjectChanges
+				.calledWith(@project_id)
 				.should.equal true
 
 		it "should record the error", ->