Skip to content

Commit 9f41521

Browse files
committed
fix(backfill): fix async handling in firestore to typesense backfill
- Modify `typesenseDocumentFromSnapshot` calls to use `Promise.all`, ensuring proper handling of asynchronous operations during the backfill process from Firestore to Typesense.
1 parent 4f6cd09 commit 9f41521

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

functions/src/backfill.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,19 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
3636
`into Typesense Collection ${config.typesenseCollectionName} ` +
3737
`on ${config.typesenseHosts.join(",")}`);
3838

39-
if (!validateBackfillRun(snapshot)) {
40-
return false;
41-
}
39+
if (!validateBackfillRun(snapshot)) {
40+
return false;
41+
}
4242

4343
const typesense = createTypesenseClient();
4444

45-
const querySnapshot =
46-
await admin.firestore().collection(config.firestoreCollectionPath);
47-
45+
const querySnapshot = admin.firestore().collection(config.firestoreCollectionPath);
46+
4847
let lastDoc = null;
4948

5049
do {
51-
const queryFotThisBatch = lastDoc ? querySnapshot.startAfter(lastDoc) : querySnapshot;
52-
const thisBatch = await queryFotThisBatch.limit(config.typesenseBackfillBatchSize).get();
50+
const queryForThisBatch = lastDoc ? querySnapshot.startAfter(lastDoc) : querySnapshot;
51+
const thisBatch = await queryForThisBatch.limit(config.typesenseBackfillBatchSize).get();
5352
if (thisBatch.empty) {
5453
break;
5554
}
@@ -65,13 +64,9 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
6564
.import(currentDocumentsBatch);
6665
functions.logger.info(`Imported ${currentDocumentsBatch.length} documents into Typesense`);
6766
} catch (error) {
68-
if (error.importResults) {
69-
const failedItems = error.importResults.filter(
70-
(r) => r.success === false,
71-
);
72-
functions.logger.error("Import failed with document errors", failedItems);
73-
} else {
74-
functions.logger.error("Import error", error);
67+
functions.logger.error(`Import error in a batch of documents from ${currentDocumentsBatch[0].id} to ${lastDoc.id}`, error);
68+
if ("importResults" in error) {
69+
logImportErrors(error.importResults);
7570
}
7671
}
7772

@@ -83,16 +78,8 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
8378
await new Promise((resolve) => process.nextTick(resolve));
8479
} while (lastDoc);
8580

86-
if (currentDocumentsBatch.length < config.typesenseBackfillBatchSize) {
87-
break;
88-
}
89-
// Recurse on the next process tick, to avoid
90-
// issues with the event loop on firebase functions related to resource release
91-
await new Promise((resolve) => process.nextTick(resolve));
92-
}
93-
94-
functions.logger.info("Done backfilling to Typesense from Firestore");
95-
});
81+
functions.logger.info("Done backfilling to Typesense from Firestore");
82+
});
9683

9784
function logImportErrors(importResults) {
9885
importResults.forEach((result) => {

functions/src/backfillToTypesenseFromFirestore.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ module.exports = functions.handler.firestore.document
5050
if (thisBatch.empty) {
5151
break;
5252
}
53-
const currentDocumentsBatch = thisBatch.docs.map((doc) => utils.typesenseDocumentFromSnapshot(doc));
53+
const currentDocumentsBatch = await Promise.all(thisBatch.docs.map(async (doc) => {
54+
return await utils.typesenseDocumentFromSnapshot(doc);
55+
}));
5456

5557
lastDoc = thisBatch.docs.at(-1) ?? null;
5658
try {

0 commit comments

Comments
 (0)