Browse Source

Merge pull request #23977 from overleaf/bg-backup-worker-load-global-blobs

ensure global blobs are loaded in backup worker

GitOrigin-RevId: b80791ef38e7fa8db364bdd3a9c461757778409d
Brian Gough 1 year ago
parent
commit
8d0fa5be65
1 changed files with 12 additions and 1 deletions
  1. 12 1
      services/history-v1/storage/scripts/backup.mjs

+ 12 - 1
services/history-v1/storage/scripts/backup.mjs

@@ -43,6 +43,15 @@ import { text } from 'node:stream/consumers'
 import { fromStream as blobHashFromStream } from '../lib/blob_hash.js'
 import { fromStream as blobHashFromStream } from '../lib/blob_hash.js'
 import { NotFoundError } from '@overleaf/object-persistor/src/Errors.js'
 import { NotFoundError } from '@overleaf/object-persistor/src/Errors.js'
 
 
+// Create a singleton promise that loads global blobs once
+let globalBlobsPromise = null
+function ensureGlobalBlobsLoaded() {
+  if (!globalBlobsPromise) {
+    globalBlobsPromise = loadGlobalBlobs()
+  }
+  return globalBlobsPromise
+}
+
 EventEmitter.defaultMaxListeners = 20
 EventEmitter.defaultMaxListeners = 20
 
 
 logger.initialize('history-v1-backup')
 logger.initialize('history-v1-backup')
@@ -497,6 +506,7 @@ function makeChunkKey(projectId, startVersion) {
 }
 }
 
 
 export async function backupProject(projectId, options) {
 export async function backupProject(projectId, options) {
+  await ensureGlobalBlobsLoaded()
   // FIXME: flush the project first!
   // FIXME: flush the project first!
   // Let's assume the the flush happens externally and triggers this backup
   // Let's assume the the flush happens externally and triggers this backup
   const backupStartTime = new Date()
   const backupStartTime = new Date()
@@ -641,6 +651,7 @@ function convertToISODate(dateStr) {
 }
 }
 
 
 export async function initializeProjects(options) {
 export async function initializeProjects(options) {
+  await ensureGlobalBlobsLoaded()
   const limiter = pLimit(BATCH_CONCURRENCY)
   const limiter = pLimit(BATCH_CONCURRENCY)
 
 
   async function processBatch(batch) {
   async function processBatch(batch) {
@@ -920,7 +931,7 @@ async function compareAllProjects(options) {
 
 
 async function main() {
 async function main() {
   const options = handleOptions()
   const options = handleOptions()
-  await loadGlobalBlobs()
+  await ensureGlobalBlobsLoaded()
   const projectId = options.projectId
   const projectId = options.projectId
 
 
   if (options.status) {
   if (options.status) {