Merge pull request #2493 from overleaf/ta-system-messages-refresh

Refresh System Messages at Regular Interval

GitOrigin-RevId: 39728c89ca61905a2166a90b083834e6b2c61316
This commit is contained in:
Timothée Alby 2020-01-07 12:03:43 +01:00 committed by Copybot
parent 850d5f957c
commit bf05d23793
2 changed files with 22 additions and 53 deletions

View file

@ -17,17 +17,7 @@ module.exports = SystemMessageManager = {
if (callback == null) {
callback = function(error, messages) {}
}
if (this._cachedMessages != null) {
return callback(null, this._cachedMessages)
} else {
return this.getMessagesFromDB((error, messages) => {
if (error != null) {
return callback(error)
}
this._cachedMessages = messages
return callback(null, messages)
})
}
callback(null, this._cachedMessages)
},
getMessagesFromDB(callback) {
@ -52,10 +42,15 @@ module.exports = SystemMessageManager = {
return message.save(callback)
},
clearCache() {
return delete this._cachedMessages
refreshCache() {
this.getMessagesFromDB((error, messages) => {
if (!error) {
this._cachedMessages = messages
}
})
}
}
const CACHE_TIMEOUT = 20 * 1000 // 20 seconds
setInterval(() => SystemMessageManager.clearCache(), CACHE_TIMEOUT)
const CACHE_TIMEOUT = 10 * 1000 * (Math.random() + 2) // 20-30 seconds
SystemMessageManager.refreshCache()
setInterval(() => SystemMessageManager.refreshCache(), CACHE_TIMEOUT)

View file

@ -21,7 +21,10 @@ const modulePath = require('path').join(
describe('SystemMessageManager', function() {
beforeEach(function() {
this.SystemMessage = {}
this.messages = ['messages-stub']
this.SystemMessage = {
find: sinon.stub().yields(null, this.messages)
}
this.SystemMessageManager = SandboxedModule.require(modulePath, {
globals: {
console: console
@ -33,47 +36,18 @@ describe('SystemMessageManager', function() {
return (this.callback = sinon.stub())
})
it('should look the messages up in the database on import', function() {
sinon.assert.called(this.SystemMessage.find)
})
describe('getMessage', function() {
beforeEach(function() {
this.messages = ['messages-stub']
return (this.SystemMessage.find = sinon
.stub()
.callsArgWith(1, null, this.messages))
this.SystemMessageManager._cachedMessages = this.messages
return this.SystemMessageManager.getMessages(this.callback)
})
describe('when the messages are not cached', function() {
beforeEach(function() {
return this.SystemMessageManager.getMessages(this.callback)
})
it('should look the messages up in the database', function() {
return this.SystemMessage.find.calledWith({}).should.equal(true)
})
it('should return the messages', function() {
return this.callback.calledWith(null, this.messages).should.equal(true)
})
it('should cache the messages', function() {
return this.SystemMessageManager._cachedMessages.should.equal(
this.messages
)
})
})
describe('when the messages are cached', function() {
beforeEach(function() {
this.SystemMessageManager._cachedMessages = this.messages
return this.SystemMessageManager.getMessages(this.callback)
})
it('should not look the messages up in the database', function() {
return this.SystemMessage.find.called.should.equal(false)
})
it('should return the messages', function() {
return this.callback.calledWith(null, this.messages).should.equal(true)
})
it('should return the messages', function() {
return this.callback.calledWith(null, this.messages).should.equal(true)
})
})