From 2cd85fefec9ce8586958cfa75a1786ad8d745145 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 21 Mar 2014 13:48:14 +0000 Subject: [PATCH] Flush all docs in a project before getting updates --- .../app/coffee/RedisManager.coffee | 3 ++ .../app/coffee/UpdatesManager.coffee | 14 ++++++++- .../coffee/GettingUpdatesTests.coffee | 4 +-- .../RedisManager/RedisManagerTests.coffee | 14 +++++++++ .../UpdatesManager/UpdatesManagerTests.coffee | 29 +++++++++++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/services/track-changes/app/coffee/RedisManager.coffee b/services/track-changes/app/coffee/RedisManager.coffee index 98f310a9f8..676259d311 100644 --- a/services/track-changes/app/coffee/RedisManager.coffee +++ b/services/track-changes/app/coffee/RedisManager.coffee @@ -26,3 +26,6 @@ module.exports = RedisManager = multi.exec (error, results) -> return callback(error) if error? callback null + + getDocIdsWithHistoryOps: (project_id, callback = (error, doc_ids) ->) -> + rclient.smembers docsWithHistoryOpsKey(project_id), callback diff --git a/services/track-changes/app/coffee/UpdatesManager.coffee b/services/track-changes/app/coffee/UpdatesManager.coffee index e582108e29..42cd38fad4 100644 --- a/services/track-changes/app/coffee/UpdatesManager.coffee +++ b/services/track-changes/app/coffee/UpdatesManager.coffee @@ -64,6 +64,16 @@ module.exports = UpdatesManager = 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) ->) -> UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, (error) -> return callback(error) if error? @@ -77,7 +87,9 @@ module.exports = UpdatesManager = callback null, 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) ->) -> UpdatesManager.getProjectUpdates project_id, options, (error, updates) -> diff --git a/services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee b/services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee index 8319c80456..cbf93f105a 100644 --- a/services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee +++ b/services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee @@ -43,9 +43,7 @@ describe "Getting updates", -> TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) => throw error if error? - TrackChangesClient.flushUpdates @project_id, @doc_id, (error) => - throw error if error? - done() + done() after: () -> MockWebApi.getUser.restore() diff --git a/services/track-changes/test/unit/coffee/RedisManager/RedisManagerTests.coffee b/services/track-changes/test/unit/coffee/RedisManager/RedisManagerTests.coffee index 27c829a948..5efb9768f6 100644 --- a/services/track-changes/test/unit/coffee/RedisManager/RedisManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/RedisManager/RedisManagerTests.coffee @@ -52,3 +52,17 @@ describe "RedisManager", -> it "should call the callback ", -> @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 \ No newline at end of file diff --git a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee index c6abb6a7b6..9bdf5fd635 100644 --- a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee @@ -248,8 +248,14 @@ describe "UpdatesManager", -> @updates = ["mock-updates"] @options = { before: "mock-before", limit: "mock-limit" } @MongoManager.getProjectUpdates = sinon.stub().callsArgWith(2, null, @updates) + @UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1) @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", -> @MongoManager.getProjectUpdates .calledWith(@project_id, @options) @@ -260,6 +266,29 @@ describe "UpdatesManager", -> .calledWith(null, @updates) .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", -> beforeEach -> @updates = ["mock-updates"]