added random up to 30 mins delay to the ttl of mongo objects

This commit is contained in:
Henry Oswald 2017-01-26 13:07:54 +00:00
parent d0d7756517
commit 1d356f93ec
2 changed files with 37 additions and 2 deletions

View file

@ -526,7 +526,13 @@ module.exports = PackManager =
setTTLOnArchivedPack: (project_id, doc_id, pack_id, callback) ->
db.docHistory.findAndModify {
query: {_id: pack_id}
update: {$set: {expiresAt: new Date(Date.now() + 1*DAYS)}}
update: {$set: {expiresAt: PackManager._getOneDayInFutureWithRandomDelay()}}
}, (err) ->
logger.log {project_id, doc_id, pack_id}, "set expiry on pack"
callback()
_getOneDayInFutureWithRandomDelay: ->
thirtyMins = 1000 * 60 * 30
randomThirtyMinMax = Math.round(Math.random() * thirtyMins)
return new Date(Date.now() + randomThirtyMinMax + 1*DAYS)

View file

@ -25,7 +25,6 @@ describe "PackManager", ->
@project_id = ObjectId().toString()
@PackManager.MAX_COUNT = 512
afterEach ->
tk.reset()
@ -334,3 +333,33 @@ describe "PackManager", ->
@callback.called.should.equal true
it "should return with no error", ->
@callback.calledWith(undefined).should.equal true
describe "setTTLOnArchivedPack", ->
beforeEach ->
@pack_id = "somepackid"
@onedayinms = 86400000
@db.docHistory =
findAndModify : sinon.stub().callsArgWith(1)
it "should set expires to 1 day", (done)->
@PackManager._getOneDayInFutureWithRandomDelay = sinon.stub().returns(@onedayinms)
@PackManager.setTTLOnArchivedPack @project_id, @doc_id, @pack_id, =>
args = @db.docHistory.findAndModify.args[0][0]
args.query._id.should.equal @pack_id
args.update['$set'].expiresAt.should.equal @onedayinms
done()
describe "_getOneDayInFutureWithRandomDelay", ->
beforeEach ->
@onedayinms = 86400000
@thirtyMins = 1000 * 60 * 30
it "should give 1 day + 30 mins random time", (done)->
loops = 10000
while --loops > 0
randomDelay = @PackManager._getOneDayInFutureWithRandomDelay() - new Date(Date.now() + @onedayinms)
randomDelay.should.be.above(0)
randomDelay.should.be.below(@thirtyMins + 1)
done()