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

include a unique id in every message published to redis

Brian Gough 7 лет назад
Родитель
Сommit
fd1425d83f

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

@@ -2,6 +2,12 @@ Settings = require('settings-sharelatex')
 rclient = require("redis-sharelatex").createClient(Settings.redis.realtime)
 Keys = Settings.redis.realtime.key_schema
 logger = require('logger-sharelatex')
+os = require "os"
+crypto = require "crypto"
+
+HOST = os.hostname()
+RND = crypto.randomBytes(4).toString('hex') # generate a random key for this process
+COUNT = 0
 
 MAX_OPS_PER_ITERATION = 8 # process a limited number of ops for safety
 
@@ -26,4 +32,7 @@ module.exports = RealTimeRedisManager =
 		rclient.llen Keys.pendingUpdates({doc_id}), callback
 
 	sendData: (data) ->
+		# create a unique message id using a counter
+		message_id = "doc:#{HOST}:#{RND}-#{COUNT++}"
+		data?._id = message_id
 		rclient.publish "applied-ops", JSON.stringify(data)

+ 1 - 1
services/document-updater/test/acceptance/coffee/ApplyingUpdatesToADocTests.coffee

@@ -232,7 +232,7 @@ describe "Applying updates to a doc", ->
 			@messageCallback.called.should.equal true
 			[channel, message] = @messageCallback.args[0]
 			channel.should.equal "applied-ops"
-			JSON.parse(message).should.deep.equal {
+			JSON.parse(message).should.deep.include {
 				project_id: @project_id,
 				doc_id: @doc_id,
 				error:'Delete component \'not the correct content\' does not match deleted text \'one\ntwo\nthree\''

+ 12 - 0
services/document-updater/test/unit/coffee/RealTimeRedisManager/RealTimeRedisManagerTests.coffee

@@ -19,6 +19,9 @@ describe "RealTimeRedisManager", ->
 						key_schema:
 							pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
 			"logger-sharelatex": { log: () -> }
+			"crypto": @crypto = { randomBytes: sinon.stub().withArgs(4).returns(Buffer.from([0x1, 0x2, 0x3, 0x4])) }
+			"os": @os = {hostname: sinon.stub().returns("somehost")}
+
 		@doc_id = "doc-id-123"
 		@project_id = "project-id-123"
 		@callback = sinon.stub()
@@ -74,3 +77,12 @@ describe "RealTimeRedisManager", ->
 		
 		it "should return the length", ->
 			@callback.calledWith(null, @length).should.equal true
+
+	describe "sendData", ->
+		beforeEach ->
+			@message_id = "doc:somehost:01020304-0"
+			@rclient.publish = sinon.stub()
+			@RealTimeRedisManager.sendData({op: "thisop"})
+		
+		it "should send the op with a message id", ->
+			@rclient.publish.calledWith("applied-ops", JSON.stringify({op:"thisop",_id:@message_id})).should.equal true