Merge pull request #505 from sharelatex/ja-redis-cluster-refactor

Allow writing of clsi cookie cache to redis cluster secondary
This commit is contained in:
James Allen 2017-05-16 10:17:30 +01:00 committed by GitHub
commit 9d2bdcf8da
2 changed files with 29 additions and 5 deletions

View file

@ -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

View file

@ -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", ->