Răsfoiți Sursa

Log out and put the last update back when an error occurs

James Allen 12 ani în urmă
părinte
comite
188d620ce1

+ 7 - 1
services/track-changes/app/coffee/HistoryManager.coffee

@@ -21,7 +21,13 @@ module.exports = HistoryManager =
 					rawUpdates.shift()
 					rawUpdates.shift()
 
 
 				if rawUpdates[0]? and rawUpdates[0].v != lastCompressedUpdate.v + 1
 				if rawUpdates[0]? and rawUpdates[0].v != lastCompressedUpdate.v + 1
-					return callback new Error("Tried to apply raw op at version #{rawUpdates[0].v} to last compressed update with version #{lastCompressedUpdate.v}")
+					error = new Error("Tried to apply raw op at version #{rawUpdates[0].v} to last compressed update with version #{lastCompressedUpdate.v}")
+					logger.error err: error, doc_id: doc_id, "inconsistent doc versions"
+					# Push the update back into Mongo - catching errors at this
+					# point is useless, we're already bailing
+					MongoManager.insertCompressedUpdates doc_id, [lastCompressedUpdate], () ->
+						return callback error
+					return
 
 
 			compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
 			compressedUpdates = UpdateCompressor.compressRawUpdates lastCompressedUpdate, rawUpdates
 			MongoManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->
 			MongoManager.insertCompressedUpdates doc_id, compressedUpdates, (error) ->

+ 7 - 1
services/track-changes/test/unit/coffee/HistoryManager/HistoryManagerTests.coffee

@@ -12,7 +12,7 @@ describe "HistoryManager", ->
 			"./MongoManager" : @MongoManager = {}
 			"./MongoManager" : @MongoManager = {}
 			"./RedisManager" : @RedisManager = {}
 			"./RedisManager" : @RedisManager = {}
 			"./LockManager"  : @LockManager = {}
 			"./LockManager"  : @LockManager = {}
-			"logger-sharelatex": { log: sinon.stub() }
+			"logger-sharelatex": { log: sinon.stub(), error: sinon.stub() }
 		@doc_id = "doc-id-123"
 		@doc_id = "doc-id-123"
 		@callback = sinon.stub()
 		@callback = sinon.stub()
 
 
@@ -111,6 +111,12 @@ describe "HistoryManager", ->
 						.calledWith(new Error("Tried to apply raw op at version 13 to last compressed update with version 11"))
 						.calledWith(new Error("Tried to apply raw op at version 13 to last compressed update with version 11"))
 						.should.equal true
 						.should.equal true
 
 
+				it "should put the popped update back into mongo", ->
+					@MongoManager.insertCompressedUpdates.calledOnce.should.equal true
+					@MongoManager.insertCompressedUpdates
+						.calledWith(@doc_id, [@lastCompressedUpdate])
+						.should.equal true
+
 	describe "processUncompressedUpdates", ->
 	describe "processUncompressedUpdates", ->
 		describe "when there is fewer than one batch to send", ->
 		describe "when there is fewer than one batch to send", ->
 			beforeEach ->
 			beforeEach ->