From 61b7bea203aaa523dfec86558a6bf5194b24e202 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Mon, 14 Jul 2014 13:20:29 +0100 Subject: [PATCH] added ttl to the users set to keep redis clean from inactive projects --- .../ConnectedUsers/ConnectedUsersManager.coffee | 9 +++++++-- .../ConnectedUsersManagerTests.coffee | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee b/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee index c275eabcba..72254c6dd1 100644 --- a/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee +++ b/services/web/app/coffee/Features/ConnectedUsers/ConnectedUsersManager.coffee @@ -8,6 +8,8 @@ rclient.auth(Settings.redis.web.password) ONE_HOUR_IN_S = 60 * 60 +ONE_DAY_IN_S = ONE_HOUR_IN_S * 24 +FOUR_DAYS_IN_S = ONE_DAY_IN_S * 4 buildProjectSetKey = (project_id)-> return "users_in_project:#{project_id}" buildUserKey = (project_id, user_id)-> return "connected_user:#{project_id}:#{user_id}" @@ -21,8 +23,9 @@ module.exports = (cb)-> rclient.sadd buildProjectSetKey(project_id), user_id, cb (cb)-> - ttl = ONE_HOUR_IN_S * 6 - rclient.setex buildUserKey(project_id, user_id), ttl, new Date(), cb + rclient.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S, cb + (cb)-> + rclient.setex buildUserKey(project_id, user_id), ONE_HOUR_IN_S, new Date(), cb ], (err)-> if err? logger.err err:err, project_id:project_id, user_id:user_id, "problem marking user as connected" @@ -33,6 +36,8 @@ module.exports = async.series [ (cb)-> rclient.srem buildProjectSetKey(project_id), user_id, cb + (cb)-> + rclient.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S, cb (cb)-> rclient.del buildUserKey(project_id, user_id), cb ], callback diff --git a/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee b/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee index 00b2457b0b..b15e314c51 100644 --- a/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/ConnectedUsersManager/ConnectedUsersManagerTests.coffee @@ -24,6 +24,7 @@ describe "ConnectedUsersManager", -> srem:sinon.stub() del:sinon.stub() smembers:sinon.stub() + expire:sinon.stub() tk.freeze(new Date()) @ConnectedUsersManager = SandboxedModule.require modulePath, requires: @@ -41,12 +42,12 @@ describe "ConnectedUsersManager", -> beforeEach -> @rClient.setex.callsArgWith(3) @rClient.sadd.callsArgWith(2) + @rClient.expire.callsArgWith(2) it "should set a key with the date and give it a ttl", (done)-> @ConnectedUsersManager.markUserAsConnected @project_id, @user_id, (err)=> - console.log @rClient.setex.args[0], "connected_user:#{@project_id}:#{@user_id}" - @rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", 60 * 60 * 6, new Date()).should.equal true + @rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", 60 * 60, new Date()).should.equal true done() it "should push the user_id on to the project list", (done)-> @@ -54,10 +55,16 @@ describe "ConnectedUsersManager", -> @rClient.sadd.calledWith("users_in_project:#{@project_id}", @user_id).should.equal true done() + it "should add a ttl to the connected user set so it stays clean", (done)-> + @ConnectedUsersManager.markUserAsConnected @project_id, @user_id, (err)=> + @rClient.expire.calledWith("users_in_project:#{@project_id}", 24 * 4 * 60 * 60).should.equal true + done() + describe "markUserAsDisconnected", -> beforeEach -> @rClient.srem.callsArgWith(2) @rClient.del.callsArgWith(1) + @rClient.expire.callsArgWith(2) it "should remove the user from the set", (done)-> @ConnectedUsersManager.markUserAsDisconnected @project_id, @user_id, (err)=> @@ -69,6 +76,10 @@ describe "ConnectedUsersManager", -> @rClient.del.calledWith("connected_user:#{@project_id}:#{@user_id}").should.equal true done() + it "should add a ttl to the connected user set so it stays clean", (done)-> + @ConnectedUsersManager.markUserAsDisconnected @project_id, @user_id, (err)=> + @rClient.expire.calledWith("users_in_project:#{@project_id}", 24 * 4 * 60 * 60).should.equal true + done() describe "_getConnectedUser", ->