Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf: prevent duplicated parallel executions of cloud metering job (#4455)

Marc Klingen851de49789fd
diff --git a/worker/src/ee/cloudUsageMetering/handleCloudUsageMeteringJob.ts b/worker/src/ee/cloudUsageMetering/handleCloudUsageMeteringJob.ts
index 90cf9519f3..809a57c58b 100644
--- a/worker/src/ee/cloudUsageMetering/handleCloudUsageMeteringJob.ts
+++ b/worker/src/ee/cloudUsageMetering/handleCloudUsageMeteringJob.ts
@@ -68,13 +68,26 @@ export const handleCloudUsageMeteringJob = async (job: Job) => {
}
}
- await prisma.cronJobs.update({
- where: { name: cloudUsageMeteringDbCronJobName },
- data: {
- state: CloudUsageMeteringDbCronJobStates.Processing,
- jobStartedAt: new Date(),
- },
- });
+ try {
+ await prisma.cronJobs.update({
+ where: {
+ name: cloudUsageMeteringDbCronJobName,
+ state: CloudUsageMeteringDbCronJobStates.Queued,
+ },
+ data: {
+ state: CloudUsageMeteringDbCronJobStates.Processing,
+ jobStartedAt: new Date(),
+ },
+ });
+ } catch (e) {
+ logger.warn(
+ "[CLOUD USAGE METERING] Failed to update cron job state, potential race condition, exiting",
+ {
+ e,
+ },
+ );
+ return;
+ }
// timing
const meterIntervalStart = cron.lastRun;