瀏覽代碼

Flush project changes when queue hits limit

Michael Walker 8 年之前
父節點
當前提交
24c74db0dc

+ 7 - 4
services/document-updater/app/coffee/HistoryManager.coffee

@@ -42,10 +42,7 @@ module.exports = HistoryManager =
 
 		if Settings.apis?.project_history?.enabled
 			if HistoryManager._shouldFlushHistoryOps(project_ops_length, ops, HistoryManager.FLUSH_PROJECT_EVERY_N_OPS)
-				# Do this in the background since it uses HTTP and so may be too
-				# slow to wait for when processing a doc update.
-				logger.log { project_ops_length, project_id }, "flushing project history api"
-				HistoryManager._flushProjectChangesAsync project_id
+				HistoryManager.flushProjectChanges project_id, project_ops_length
 
 		HistoryRedisManager.recordDocHasHistoryOps project_id, doc_id, ops, (error) ->
 			return callback(error) if error?
@@ -56,6 +53,12 @@ module.exports = HistoryManager =
 				HistoryManager._flushDocChangesAsync project_id, doc_id
 			callback()
 
+	flushProjectChanges: (project_id, project_ops_length) ->
+		# Do this in the background since it uses HTTP and so may be too
+		# slow to wait for when processing a doc update.
+		logger.log { project_ops_length, project_id }, "flushing project history api"
+		HistoryManager._flushProjectChangesAsync project_id
+
 	_shouldFlushHistoryOps: (length, ops, threshold) ->
 		return false if !length # don't flush unless we know the length
 		# We want to flush every 100 ops, i.e. 100, 200, 300, etc

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

@@ -1,5 +1,6 @@
 RedisManager = require "./RedisManager"
 DocumentManager = require "./DocumentManager"
+HistoryManager = require "./HistoryManager"
 async = require "async"
 logger = require "logger-sharelatex"
 Metrics = require "./Metrics"
@@ -121,4 +122,10 @@ module.exports = ProjectManager =
 
 		async.each docUpdates, handleDocUpdate, (error) ->
 			return callback(error) if error?
-			async.each fileUpdates, handleFileUpdate, callback
+			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()