From 5aa8b71832a98654bb0e0b468bc985bfcff08bbd Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 15 May 2017 15:35:01 +0100 Subject: [PATCH] Allow writing of clsi cookie cache to redis cluster secondary --- .../Features/Compile/ClsiCookieManager.coffee | 11 +++++++-- .../Compile/ClsiCookieManagerTests.coffee | 23 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee b/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee index c19c5e02ea..4e02469f0a 100644 --- a/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee +++ b/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee @@ -2,6 +2,8 @@ Settings = require "settings-sharelatex" request = require('request') RedisWrapper = require("../../infrastructure/RedisWrapper") rclient = RedisWrapper.client("clsi_cookie") +if Settings.redis.clsi_cookie_secondary? + rclient_secondary = RedisWrapper.client("clsi_cookie_secondary") Cookie = require('cookie') logger = require "logger-sharelatex" @@ -42,11 +44,16 @@ module.exports = ClsiCookieManager = if !clsiCookiesEnabled return callback() serverId = ClsiCookieManager._parseServerIdFromResponse(response) + if rclient_secondary? + @_setServerIdInRedis rclient_secondary, project_id, serverId + @_setServerIdInRedis rclient, project_id, serverId, (err) -> + callback(err, serverId) + + _setServerIdInRedis: (rclient, project_id, serverId, callback = (err) ->) -> multi = rclient.multi() multi.set buildKey(project_id), serverId multi.expire buildKey(project_id), Settings.clsiCookie.ttl - multi.exec (err)-> - callback(err, serverId) + multi.exec callback clearServerId: (project_id, callback = (err)->)-> if !clsiCookiesEnabled diff --git a/services/web/test/UnitTests/coffee/Compile/ClsiCookieManagerTests.coffee b/services/web/test/UnitTests/coffee/Compile/ClsiCookieManagerTests.coffee index c54f405bb9..9e60d8bde7 100644 --- a/services/web/test/UnitTests/coffee/Compile/ClsiCookieManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/ClsiCookieManagerTests.coffee @@ -34,9 +34,8 @@ describe "ClsiCookieManager", -> ttl:Math.random() key: "coooookie" @requires = - "../../infrastructure/RedisWrapper": - client: => - @redis + "../../infrastructure/RedisWrapper": @RedisWrapper = + client: => @redis "settings-sharelatex": @settings "request": @request @@ -106,6 +105,24 @@ describe "ClsiCookieManager", -> @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=> @redisMulti.exec.called.should.equal false done() + + it "should also set in the secondary if secondary redis is enabled", (done) -> + @redisSecondaryMulti = + set:sinon.stub() + expire:sinon.stub() + exec:sinon.stub() + @redis_secondary = + multi: => @redisSecondaryMulti + @settings.redis.clsi_cookie_secondary = {} + @RedisWrapper.client = sinon.stub() + @RedisWrapper.client.withArgs("clsi_cookie").returns(@redis) + @RedisWrapper.client.withArgs("clsi_cookie_secondary").returns(@redis_secondary) + @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires + @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8") + @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=> + @redisSecondaryMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true + @redisSecondaryMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true + done() describe "getCookieJar", ->