Merge pull request #47 from sharelatex/bg-add-message-ids

include a unique id in every message published to redis
This commit is contained in:
Brian Gough 2019-03-22 09:07:52 +00:00 committed by GitHub
commit 1052e9e000
3 changed files with 22 additions and 1 deletions

View file

@ -2,6 +2,12 @@ Settings = require('settings-sharelatex')
rclient = require("redis-sharelatex").createClient(Settings.redis.realtime) rclient = require("redis-sharelatex").createClient(Settings.redis.realtime)
Keys = Settings.redis.realtime.key_schema Keys = Settings.redis.realtime.key_schema
logger = require('logger-sharelatex') 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 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 rclient.llen Keys.pendingUpdates({doc_id}), callback
sendData: (data) -> 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) rclient.publish "applied-ops", JSON.stringify(data)

View file

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

View file

@ -19,6 +19,9 @@ describe "RealTimeRedisManager", ->
key_schema: key_schema:
pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}" pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
"logger-sharelatex": { log: () -> } "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" @doc_id = "doc-id-123"
@project_id = "project-id-123" @project_id = "project-id-123"
@callback = sinon.stub() @callback = sinon.stub()
@ -74,3 +77,12 @@ describe "RealTimeRedisManager", ->
it "should return the length", -> it "should return the length", ->
@callback.calledWith(null, @length).should.equal true @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