From c04fea0fd388a5de70a6dfcc801fe7a2a779ae5f Mon Sep 17 00:00:00 2001 From: Jimmy Domagala-Tang Date: Wed, 26 Jun 2024 09:37:22 -0400 Subject: [PATCH] Merge pull request #19162 from overleaf/jpa-flaky-admin-test [web] refresh system message cache after creating/deleting message GitOrigin-RevId: f7155e94a8bf08b527e7ae69d9d6a41e54c01282 --- server-ce/test/admin.spec.ts | 2 -- .../SystemMessages/SystemMessageManager.js | 24 +++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/server-ce/test/admin.spec.ts b/server-ce/test/admin.spec.ts index 27f33ea5d4..c48d2cd455 100644 --- a/server-ce/test/admin.spec.ts +++ b/server-ce/test/admin.spec.ts @@ -52,8 +52,6 @@ describe('admin panel', function () { cy.get('button').contains('Post Message').click() cy.findByText(message) - cy.log('wait for system message propagation') - cy.wait(13000) const resumeUser1Session = login(user1) cy.visit('/project') cy.findByText(message) diff --git a/services/web/app/src/Features/SystemMessages/SystemMessageManager.js b/services/web/app/src/Features/SystemMessages/SystemMessageManager.js index 2eed16716a..96a6676eb2 100644 --- a/services/web/app/src/Features/SystemMessages/SystemMessageManager.js +++ b/services/web/app/src/Features/SystemMessages/SystemMessageManager.js @@ -6,6 +6,8 @@ const { callbackifyAll } = require('@overleaf/promise-utils') const logger = require('@overleaf/logger') const SystemMessageManager = { + _cachedMessages: [], + getMessages() { return this._cachedMessages }, @@ -16,28 +18,30 @@ const SystemMessageManager = { async clearMessages() { await SystemMessage.deleteMany({}).exec() + await this.refreshCache() }, async createMessage(content) { const message = new SystemMessage({ content }) await message.save() + await this.refreshCache() }, - refreshCache() { - this.getMessagesFromDB() - .then(messages => { - this._cachedMessages = messages - }) - .catch(err => { - logger.warn({ err }, 'failed to refresh system messages cache') - }) + async refreshCache() { + this._cachedMessages = await this.getMessagesFromDB() + }, + + refreshCacheInBackground() { + this.refreshCache().catch(err => { + logger.warn({ err }, 'failed to refresh system messages cache') + }) }, } const CACHE_TIMEOUT = 10 * 1000 * (Math.random() + 2) // 20-30 seconds -SystemMessageManager.refreshCache() +SystemMessageManager.refreshCacheInBackground() const intervalHandle = setInterval( - () => SystemMessageManager.refreshCache(), + () => SystemMessageManager.refreshCacheInBackground(), CACHE_TIMEOUT )