added ttl to the users set to keep redis clean from inactive projects

This commit is contained in:
Henry Oswald 2014-07-14 13:20:29 +01:00
parent 2b2b995340
commit 61b7bea203
2 changed files with 20 additions and 4 deletions

View file

@ -8,6 +8,8 @@ rclient.auth(Settings.redis.web.password)
ONE_HOUR_IN_S = 60 * 60 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}" buildProjectSetKey = (project_id)-> return "users_in_project:#{project_id}"
buildUserKey = (project_id, user_id)-> return "connected_user:#{project_id}:#{user_id}" buildUserKey = (project_id, user_id)-> return "connected_user:#{project_id}:#{user_id}"
@ -21,8 +23,9 @@ module.exports =
(cb)-> (cb)->
rclient.sadd buildProjectSetKey(project_id), user_id, cb rclient.sadd buildProjectSetKey(project_id), user_id, cb
(cb)-> (cb)->
ttl = ONE_HOUR_IN_S * 6 rclient.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S, cb
rclient.setex buildUserKey(project_id, user_id), ttl, new Date(), cb (cb)->
rclient.setex buildUserKey(project_id, user_id), ONE_HOUR_IN_S, new Date(), cb
], (err)-> ], (err)->
if err? if err?
logger.err err:err, project_id:project_id, user_id:user_id, "problem marking user as connected" 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 [ async.series [
(cb)-> (cb)->
rclient.srem buildProjectSetKey(project_id), user_id, cb rclient.srem buildProjectSetKey(project_id), user_id, cb
(cb)->
rclient.expire buildProjectSetKey(project_id), FOUR_DAYS_IN_S, cb
(cb)-> (cb)->
rclient.del buildUserKey(project_id, user_id), cb rclient.del buildUserKey(project_id, user_id), cb
], callback ], callback

View file

@ -24,6 +24,7 @@ describe "ConnectedUsersManager", ->
srem:sinon.stub() srem:sinon.stub()
del:sinon.stub() del:sinon.stub()
smembers:sinon.stub() smembers:sinon.stub()
expire:sinon.stub()
tk.freeze(new Date()) tk.freeze(new Date())
@ConnectedUsersManager = SandboxedModule.require modulePath, requires: @ConnectedUsersManager = SandboxedModule.require modulePath, requires:
@ -41,12 +42,12 @@ describe "ConnectedUsersManager", ->
beforeEach -> beforeEach ->
@rClient.setex.callsArgWith(3) @rClient.setex.callsArgWith(3)
@rClient.sadd.callsArgWith(2) @rClient.sadd.callsArgWith(2)
@rClient.expire.callsArgWith(2)
it "should set a key with the date and give it a ttl", (done)-> it "should set a key with the date and give it a ttl", (done)->
@ConnectedUsersManager.markUserAsConnected @project_id, @user_id, (err)=> @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, new Date()).should.equal true
@rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", 60 * 60 * 6, new Date()).should.equal true
done() done()
it "should push the user_id on to the project list", (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 @rClient.sadd.calledWith("users_in_project:#{@project_id}", @user_id).should.equal true
done() 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", -> describe "markUserAsDisconnected", ->
beforeEach -> beforeEach ->
@rClient.srem.callsArgWith(2) @rClient.srem.callsArgWith(2)
@rClient.del.callsArgWith(1) @rClient.del.callsArgWith(1)
@rClient.expire.callsArgWith(2)
it "should remove the user from the set", (done)-> it "should remove the user from the set", (done)->
@ConnectedUsersManager.markUserAsDisconnected @project_id, @user_id, (err)=> @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 @rClient.del.calledWith("connected_user:#{@project_id}:#{@user_id}").should.equal true
done() 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", -> describe "_getConnectedUser", ->