ソースを参照

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

Allow writing of clsi cookie cache to redis cluster secondary
James Allen 9 年 前
親
コミット
9d2bdcf8da

+ 9 - 2
services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee

@@ -2,6 +2,8 @@ Settings = require "settings-sharelatex"
 request = require('request')
 request = require('request')
 RedisWrapper = require("../../infrastructure/RedisWrapper")
 RedisWrapper = require("../../infrastructure/RedisWrapper")
 rclient = RedisWrapper.client("clsi_cookie")
 rclient = RedisWrapper.client("clsi_cookie")
+if Settings.redis.clsi_cookie_secondary?
+	rclient_secondary = RedisWrapper.client("clsi_cookie_secondary")
 Cookie = require('cookie')
 Cookie = require('cookie')
 logger = require "logger-sharelatex"
 logger = require "logger-sharelatex"
 
 
@@ -42,11 +44,16 @@ module.exports = ClsiCookieManager =
 		if !clsiCookiesEnabled
 		if !clsiCookiesEnabled
 			return callback()
 			return callback()
 		serverId = ClsiCookieManager._parseServerIdFromResponse(response)
 		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 = rclient.multi()
 		multi.set buildKey(project_id), serverId
 		multi.set buildKey(project_id), serverId
 		multi.expire buildKey(project_id), Settings.clsiCookie.ttl
 		multi.expire buildKey(project_id), Settings.clsiCookie.ttl
-		multi.exec (err)->
-			callback(err, serverId)
+		multi.exec callback
 
 
 	clearServerId: (project_id, callback = (err)->)->
 	clearServerId: (project_id, callback = (err)->)->
 		if !clsiCookiesEnabled
 		if !clsiCookiesEnabled

+ 20 - 3
services/web/test/UnitTests/coffee/Compile/ClsiCookieManagerTests.coffee

@@ -34,9 +34,8 @@ describe "ClsiCookieManager", ->
 				ttl:Math.random()
 				ttl:Math.random()
 				key: "coooookie"
 				key: "coooookie"
 		@requires = 
 		@requires = 
-			"../../infrastructure/RedisWrapper":
-				client: =>
-					@redis
+			"../../infrastructure/RedisWrapper": @RedisWrapper =
+				client: => @redis
 			"settings-sharelatex": @settings
 			"settings-sharelatex": @settings
 			"request": @request
 			"request": @request
 
 
@@ -106,6 +105,24 @@ describe "ClsiCookieManager", ->
 			@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
 			@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
 				@redisMulti.exec.called.should.equal false
 				@redisMulti.exec.called.should.equal false
 				done()
 				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", ->
 	describe "getCookieJar", ->