Sfoglia il codice sorgente

Flush all docs in a project before getting updates

James Allen 12 anni fa
parent
commit
2cd85fefec

+ 3 - 0
services/track-changes/app/coffee/RedisManager.coffee

@@ -26,3 +26,6 @@ module.exports = RedisManager =
 		multi.exec (error, results) ->
 		multi.exec (error, results) ->
 			return callback(error) if error?
 			return callback(error) if error?
 			callback null
 			callback null
+
+	getDocIdsWithHistoryOps: (project_id, callback = (error, doc_ids) ->) ->
+		rclient.smembers docsWithHistoryOpsKey(project_id), callback

+ 13 - 1
services/track-changes/app/coffee/UpdatesManager.coffee

@@ -64,6 +64,16 @@ module.exports = UpdatesManager =
 			callback
 			callback
 		)
 		)
 
 
+	processUncompressedUpdatesForProject: (project_id, callback = (error) ->) ->
+		RedisManager.getDocIdsWithHistoryOps project_id, (error, doc_ids) ->
+			return callback(error) if error?
+			jobs = []
+			for doc_id in doc_ids
+				do (doc_id) ->
+					jobs.push (callback) ->
+						UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, callback
+			async.parallelLimit jobs, 5, callback
+
 	getDocUpdates: (project_id, doc_id, options = {}, callback = (error, updates) ->) ->
 	getDocUpdates: (project_id, doc_id, options = {}, callback = (error, updates) ->) ->
 		UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, (error) ->
 		UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, (error) ->
 			return callback(error) if error?
 			return callback(error) if error?
@@ -77,7 +87,9 @@ module.exports = UpdatesManager =
 				callback null, updates
 				callback null, updates
 
 
 	getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
 	getProjectUpdates: (project_id, options = {}, callback = (error, updates) ->) ->
-		MongoManager.getProjectUpdates project_id, options, callback
+		UpdatesManager.processUncompressedUpdatesForProject project_id, (error) ->
+			return callback(error) if error?
+			MongoManager.getProjectUpdates project_id, options, callback
 
 
 	getProjectUpdatesWithUserInfo: (project_id, options = {}, callback = (error, updates) ->) ->
 	getProjectUpdatesWithUserInfo: (project_id, options = {}, callback = (error, updates) ->) ->
 		UpdatesManager.getProjectUpdates project_id, options, (error, updates) ->
 		UpdatesManager.getProjectUpdates project_id, options, (error, updates) ->

+ 1 - 3
services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee

@@ -43,9 +43,7 @@ describe "Getting updates", ->
 
 
 		TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) =>
 		TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) =>
 			throw error if error?
 			throw error if error?
-			TrackChangesClient.flushUpdates @project_id, @doc_id, (error) =>
-				throw error if error?
-				done()
+			done()
 
 
 	after: () ->
 	after: () ->
 		MockWebApi.getUser.restore()
 		MockWebApi.getUser.restore()

+ 14 - 0
services/track-changes/test/unit/coffee/RedisManager/RedisManagerTests.coffee

@@ -52,3 +52,17 @@ describe "RedisManager", ->
 
 
 		it "should call the callback ", ->
 		it "should call the callback ", ->
 			@callback.called.should.equal true
 			@callback.called.should.equal true
+
+	describe "getDocIdsWithHistoryOps", ->
+		beforeEach ->
+			@doc_ids = ["mock-id-1", "mock-id-2"]
+			@rclient.smembers = sinon.stub().callsArgWith(1, null, @doc_ids)
+			@RedisManager.getDocIdsWithHistoryOps @project_id, @callback
+
+		it "should read the doc_ids from redis", ->
+			@rclient.smembers
+				.calledWith("DocsWithHistoryOps:#{@project_id}")
+				.should.equal true
+
+		it "should call the callback with the doc_ids", ->
+			@callback.calledWith(null, @doc_ids).should.equal true

+ 29 - 0
services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee

@@ -248,8 +248,14 @@ describe "UpdatesManager", ->
 			@updates = ["mock-updates"]
 			@updates = ["mock-updates"]
 			@options = { before: "mock-before", limit: "mock-limit" }
 			@options = { before: "mock-before", limit: "mock-limit" }
 			@MongoManager.getProjectUpdates = sinon.stub().callsArgWith(2, null, @updates)
 			@MongoManager.getProjectUpdates = sinon.stub().callsArgWith(2, null, @updates)
+			@UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
 			@UpdatesManager.getProjectUpdates @project_id, @options, @callback
 			@UpdatesManager.getProjectUpdates @project_id, @options, @callback
 
 
+		it "should process any outstanding updates", ->
+			@UpdatesManager.processUncompressedUpdatesForProject
+				.calledWith(@project_id)
+				.should.equal true
+
 		it "should get the updates from the database", ->
 		it "should get the updates from the database", ->
 			@MongoManager.getProjectUpdates
 			@MongoManager.getProjectUpdates
 				.calledWith(@project_id, @options)
 				.calledWith(@project_id, @options)
@@ -260,6 +266,29 @@ describe "UpdatesManager", ->
 				.calledWith(null, @updates)
 				.calledWith(null, @updates)
 				.should.equal true
 				.should.equal true
 
 
+	describe "processUncompressedUpdatesForProject", ->
+		beforeEach (done) ->
+			@doc_ids = ["mock-id-1", "mock-id-2"]
+			@UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
+			@RedisManager.getDocIdsWithHistoryOps = sinon.stub().callsArgWith(1, null, @doc_ids)
+			@UpdatesManager.processUncompressedUpdatesForProject @project_id, () =>
+				@callback()
+				done()
+
+		it "should get all the docs with history ops", ->
+			@RedisManager.getDocIdsWithHistoryOps
+				.calledWith(@project_id)
+				.should.equal true
+
+		it "should process the doc ops for the each doc_id", ->
+			for doc_id in @doc_ids
+				@UpdatesManager.processUncompressedUpdatesWithLock
+					.calledWith(@project_id, doc_id)
+					.should.equal true
+
+		it "should call the callback", ->
+			@callback.called.should.equal true
+
 	describe "getProjectUpdatesWithUserInfo", ->
 	describe "getProjectUpdatesWithUserInfo", ->
 		beforeEach ->
 		beforeEach ->
 			@updates = ["mock-updates"]
 			@updates = ["mock-updates"]