Răsfoiți Sursa

Merge pull request #29718 from overleaf/bg-history-fix-backup-errors

fix backupBlob error handling and add tests for upload failures

GitOrigin-RevId: 3abda1b70012fda20fc403593d0ee6ce04152e24
Brian Gough 8 luni în urmă
părinte
comite
3227502aeb

+ 3 - 1
services/history-v1/storage/lib/backupBlob.mjs

@@ -240,11 +240,13 @@ export async function backupBlob(historyId, blob, tmpPath, persistor) {
       // record that we backed it up already
       await storeBlobBackup(projectId, hash)
       recordBackupConclusion('failure', 'already_backed_up')
+      // Blob already backed up so report success
       return
     }
-    // eventually queue this for retry - for now this will be fixed by running the script
     recordBackupConclusion('failure')
     logger.warn({ error, projectId, hash }, 'Failed to upload blob to backup')
+    // Always throw an exception if the blob is not backed up
+    throw error
   } finally {
     logger.debug({ projectId, hash }, 'Ended blob backup')
   }

+ 34 - 0
services/history-v1/test/acceptance/js/storage/backupBlob.test.mjs

@@ -152,6 +152,40 @@ describe('backupBlob', function () {
     })
   })
 
+  describe('when uploadBlobToBackup fails', function () {
+    let blob
+    let historyId
+    let mockPersistor
+    let uploadError
+
+    beforeEach(async function () {
+      blob = await makeBlobForFile(filePath)
+      historyId = 'abc123def456abc789def123'
+      uploadError = new Error('Upload failed')
+
+      // Create a mock persistor that rejects on sendStream
+      mockPersistor = {
+        sendStream: async () => {
+          throw uploadError
+        },
+      }
+    })
+
+    it('rethrows the error and does not record the blob as backed up', async function () {
+      await expect(
+        backupBlob(historyId, blob, filePath, mockPersistor)
+      ).to.be.rejectedWith('Upload failed')
+
+      const record = await backedUpBlobs.findOne({
+        _id: new ObjectId(historyId),
+        blobs: {
+          $elemMatch: { $eq: new Binary(Buffer.from(blob.getHash(), 'hex')) },
+        },
+      })
+      expect(record).to.not.exist
+    })
+  })
+
   const cases = [
     {
       name: 'text file',