Ver Fonte

Merge pull request #25889 from overleaf/jpa-web-wait-for-mongo

[web] wait for DB before fetching global blobs

GitOrigin-RevId: 2beefd39ae4be4d233e2aac018d471bf949faea2
Miguel Serrano há 1 ano atrás
pai
commit
923708f9f9

+ 2 - 1
services/web/app/src/Features/History/HistoryManager.js

@@ -11,7 +11,7 @@ const OError = require('@overleaf/o-error')
 const UserGetter = require('../User/UserGetter')
 const UserGetter = require('../User/UserGetter')
 const ProjectGetter = require('../Project/ProjectGetter')
 const ProjectGetter = require('../Project/ProjectGetter')
 const HistoryBackupDeletionHandler = require('./HistoryBackupDeletionHandler')
 const HistoryBackupDeletionHandler = require('./HistoryBackupDeletionHandler')
-const { db, ObjectId } = require('../../infrastructure/mongodb')
+const { db, ObjectId, waitForDb } = require('../../infrastructure/mongodb')
 const Metrics = require('@overleaf/metrics')
 const Metrics = require('@overleaf/metrics')
 const logger = require('@overleaf/logger')
 const logger = require('@overleaf/logger')
 const { NotFoundError } = require('../Errors/Errors')
 const { NotFoundError } = require('../Errors/Errors')
@@ -50,6 +50,7 @@ function getBlobLocation(projectId, hash) {
 }
 }
 
 
 async function loadGlobalBlobs() {
 async function loadGlobalBlobs() {
+  await waitForDb() // CHANGE FROM SOURCE: wait for db before running query.
   const blobs = db.projectHistoryGlobalBlobs.find()
   const blobs = db.projectHistoryGlobalBlobs.find()
   for await (const blob of blobs) {
   for await (const blob of blobs) {
     GLOBAL_BLOBS.add(blob._id) // CHANGE FROM SOURCE: only store hashes.
     GLOBAL_BLOBS.add(blob._id) // CHANGE FROM SOURCE: only store hashes.

+ 5 - 0
services/web/app/src/infrastructure/mongodb.js

@@ -130,10 +130,15 @@ async function getCollectionInternal(name) {
   return internalDb.collection(name)
   return internalDb.collection(name)
 }
 }
 
 
+async function waitForDb() {
+  await connectionPromise
+}
+
 module.exports = {
 module.exports = {
   db,
   db,
   ObjectId,
   ObjectId,
   connectionPromise,
   connectionPromise,
+  waitForDb,
   getCollectionNames,
   getCollectionNames,
   getCollectionInternal,
   getCollectionInternal,
   cleanupTestDatabase,
   cleanupTestDatabase,

+ 3 - 3
services/web/test/unit/src/History/HistoryManagerTests.js

@@ -3,9 +3,9 @@ const sinon = require('sinon')
 const SandboxedModule = require('sandboxed-module')
 const SandboxedModule = require('sandboxed-module')
 const { ObjectId } = require('mongodb-legacy')
 const { ObjectId } = require('mongodb-legacy')
 const {
 const {
-  connectionPromise,
   cleanupTestDatabase,
   cleanupTestDatabase,
   db,
   db,
+  waitForDb,
 } = require('../../../../app/src/infrastructure/mongodb')
 } = require('../../../../app/src/infrastructure/mongodb')
 
 
 const MODULE_PATH = '../../../../app/src/Features/History/HistoryManager'
 const MODULE_PATH = '../../../../app/src/Features/History/HistoryManager'
@@ -19,7 +19,7 @@ const GLOBAL_BLOBS = {
 
 
 describe('HistoryManager', function () {
 describe('HistoryManager', function () {
   before(async function () {
   before(async function () {
-    await connectionPromise
+    await waitForDb()
   })
   })
   before(cleanupTestDatabase)
   before(cleanupTestDatabase)
   before(async function () {
   before(async function () {
@@ -90,7 +90,7 @@ describe('HistoryManager', function () {
 
 
     this.HistoryManager = SandboxedModule.require(MODULE_PATH, {
     this.HistoryManager = SandboxedModule.require(MODULE_PATH, {
       requires: {
       requires: {
-        '../../infrastructure/mongodb': { ObjectId, db },
+        '../../infrastructure/mongodb': { ObjectId, db, waitForDb },
         '@overleaf/fetch-utils': this.FetchUtils,
         '@overleaf/fetch-utils': this.FetchUtils,
         '@overleaf/settings': this.settings,
         '@overleaf/settings': this.settings,
         '../User/UserGetter': this.UserGetter,
         '../User/UserGetter': this.UserGetter,