From fe124e565636f68136f2561030fbb9930e2d8777 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Fri, 23 May 2025 12:46:16 +0200 Subject: [PATCH] Merge pull request #25889 from overleaf/jpa-web-wait-for-mongo [web] wait for DB before fetching global blobs GitOrigin-RevId: 2beefd39ae4be4d233e2aac018d471bf949faea2 --- services/web/app/src/Features/History/HistoryManager.js | 3 ++- services/web/app/src/infrastructure/mongodb.js | 5 +++++ services/web/test/unit/src/History/HistoryManagerTests.js | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/services/web/app/src/Features/History/HistoryManager.js b/services/web/app/src/Features/History/HistoryManager.js index fe9e6e86a7..42d7e229bf 100644 --- a/services/web/app/src/Features/History/HistoryManager.js +++ b/services/web/app/src/Features/History/HistoryManager.js @@ -11,7 +11,7 @@ const OError = require('@overleaf/o-error') const UserGetter = require('../User/UserGetter') const ProjectGetter = require('../Project/ProjectGetter') const HistoryBackupDeletionHandler = require('./HistoryBackupDeletionHandler') -const { db, ObjectId } = require('../../infrastructure/mongodb') +const { db, ObjectId, waitForDb } = require('../../infrastructure/mongodb') const Metrics = require('@overleaf/metrics') const logger = require('@overleaf/logger') const { NotFoundError } = require('../Errors/Errors') @@ -50,6 +50,7 @@ function getBlobLocation(projectId, hash) { } async function loadGlobalBlobs() { + await waitForDb() // CHANGE FROM SOURCE: wait for db before running query. const blobs = db.projectHistoryGlobalBlobs.find() for await (const blob of blobs) { GLOBAL_BLOBS.add(blob._id) // CHANGE FROM SOURCE: only store hashes. diff --git a/services/web/app/src/infrastructure/mongodb.js b/services/web/app/src/infrastructure/mongodb.js index 840122791b..a3342c6575 100644 --- a/services/web/app/src/infrastructure/mongodb.js +++ b/services/web/app/src/infrastructure/mongodb.js @@ -130,10 +130,15 @@ async function getCollectionInternal(name) { return internalDb.collection(name) } +async function waitForDb() { + await connectionPromise +} + module.exports = { db, ObjectId, connectionPromise, + waitForDb, getCollectionNames, getCollectionInternal, cleanupTestDatabase, diff --git a/services/web/test/unit/src/History/HistoryManagerTests.js b/services/web/test/unit/src/History/HistoryManagerTests.js index 6b83d15ed0..aa59cda4e6 100644 --- a/services/web/test/unit/src/History/HistoryManagerTests.js +++ b/services/web/test/unit/src/History/HistoryManagerTests.js @@ -3,9 +3,9 @@ const sinon = require('sinon') const SandboxedModule = require('sandboxed-module') const { ObjectId } = require('mongodb-legacy') const { - connectionPromise, cleanupTestDatabase, db, + waitForDb, } = require('../../../../app/src/infrastructure/mongodb') const MODULE_PATH = '../../../../app/src/Features/History/HistoryManager' @@ -19,7 +19,7 @@ const GLOBAL_BLOBS = { describe('HistoryManager', function () { before(async function () { - await connectionPromise + await waitForDb() }) before(cleanupTestDatabase) before(async function () { @@ -90,7 +90,7 @@ describe('HistoryManager', function () { this.HistoryManager = SandboxedModule.require(MODULE_PATH, { requires: { - '../../infrastructure/mongodb': { ObjectId, db }, + '../../infrastructure/mongodb': { ObjectId, db, waitForDb }, '@overleaf/fetch-utils': this.FetchUtils, '@overleaf/settings': this.settings, '../User/UserGetter': this.UserGetter,