From 8b0916f8588561cf75785d44d9da9a399a85eb8f Mon Sep 17 00:00:00 2001 From: "yanlong.wang" Date: Thu, 16 May 2024 18:26:18 +0800 Subject: [PATCH] fix: race condition while logging chargeAmount --- backend/functions/src/cloud-functions/crawler.ts | 8 +++----- backend/functions/src/cloud-functions/searcher.ts | 10 +++------- thinapps-shared | 2 +- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/backend/functions/src/cloud-functions/crawler.ts b/backend/functions/src/cloud-functions/crawler.ts index 3aa0a5c..8114996 100644 --- a/backend/functions/src/cloud-functions/crawler.ts +++ b/backend/functions/src/cloud-functions/crawler.ts @@ -425,9 +425,7 @@ ${this.content} auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => { this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) }); }); - apiRoll._ref?.set({ - chargeAmount, - }, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err })); + apiRoll.chargeAmount = chargeAmount; } }); } else if (ctx.req.ip) { @@ -724,7 +722,7 @@ ${this.content} } } - getChargeAmount(formatted: { [k: string]: any; }) { + getChargeAmount(formatted: FormattedPage) { if (!formatted) { return undefined; } @@ -735,7 +733,7 @@ ${this.content} return estimateToken(textContent); } - const imageContent = formatted.screenshotUrl || formatted?.screenshot; + const imageContent = formatted.screenshotUrl || (formatted as any)?.screenshot; if (imageContent) { // OpenAI image token count for 1024x1024 image diff --git a/backend/functions/src/cloud-functions/searcher.ts b/backend/functions/src/cloud-functions/searcher.ts index 590f8ab..fa921e7 100644 --- a/backend/functions/src/cloud-functions/searcher.ts +++ b/backend/functions/src/cloud-functions/searcher.ts @@ -169,9 +169,7 @@ export class SearcherHost extends RPCHost { auth.reportUsage(chargeAmount, 'reader-search').catch((err) => { this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) }); }); - apiRoll._ref?.set({ - chargeAmount, - }, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err })); + apiRoll.chargeAmount = chargeAmount; } }); } else if (ctx.req.ip) { @@ -184,9 +182,7 @@ export class SearcherHost extends RPCHost { ); rpcReflect.finally(() => { if (chargeAmount) { - apiRoll._ref?.set({ - chargeAmount, - }, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err })); + apiRoll.chargeAmount = chargeAmount; } }); } @@ -415,7 +411,7 @@ ${this.content} return resultArray; } - getChargeAmount(formatted: any[]) { + getChargeAmount(formatted: FormattedPage[]) { return _.sum( formatted.map((x) => this.crawler.getChargeAmount(x) || 0) ); diff --git a/thinapps-shared b/thinapps-shared index 2f2cdcf..fc3545e 160000 --- a/thinapps-shared +++ b/thinapps-shared @@ -1 +1 @@ -Subproject commit 2f2cdcff7b2738be33ee5aca858ef2d65eba29ed +Subproject commit fc3545e3a7ae27968e69f351f109d3ffb535f963