Skip to content

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

fix: move early return so that pending_projects is always updates (#11679)

Valery Meleshkin88da95404f9e
diff --git a/worker/src/env.ts b/worker/src/env.ts
index 792162c847..3c2476ec49 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -361,7 +361,7 @@ const EnvSchema = z.object({
LANGFUSE_BATCH_DATA_RETENTION_CLEANER_DELETE_TIMEOUT_MS: z.coerce
.number()
.positive()
- .default(5_400_000), // 1.5 hours for DELETE operations
+ .default(10_800_000), // 3 hours for DELETE operations
// Media Retention Cleaner configuration (S3/PostgreSQL)
LANGFUSE_MEDIA_RETENTION_CLEANER_ITEM_LIMIT: z.coerce
diff --git a/worker/src/features/batch-data-retention-cleaner/index.ts b/worker/src/features/batch-data-retention-cleaner/index.ts
index 3f336a668d..d7b635caea 100644
--- a/worker/src/features/batch-data-retention-cleaner/index.ts
+++ b/worker/src/features/batch-data-retention-cleaner/index.ts
@@ -28,6 +28,7 @@ interface ProjectWorkload {
retentionDays: number;
cutoffDate: Date;
expiredRowCount: number;
+ oldestAgeSeconds: number | null;
}
/**
@@ -116,6 +117,25 @@ export class BatchDataRetentionCleaner {
return;
}
+ recordGauge(`${METRIC_PREFIX}.pending_projects`, workloads.length, {
+ table: tableName,
+ });
+
+ // Compute seconds past cutoff for each workload and find the max
+ const SECONDS_PER_DAY = 86400;
+ const maxSecondsPastCutoff = workloads
+ .filter((w) => w.oldestAgeSeconds !== null)
+ .map((w) => w.oldestAgeSeconds! - w.retentionDays * SECONDS_PER_DAY)
+ .reduce((max, val) => Math.max(max, val), -Infinity);
+
+ recordGauge(
+ `${METRIC_PREFIX}.seconds_past_cutoff`,
+ Math.max(maxSecondsPastCutoff, 0),
+ {
+ table: tableName,
+ },
+ );
+
if (workloads.length === 0) {
logger.info(
`${instanceName}: No projects with retention and data to delete`,
@@ -123,21 +143,9 @@ export class BatchDataRetentionCleaner {
return;
}
- const totalExpiredRows = workloads.reduce(
- (sum, w) => sum + w.expiredRowCount,
- 0,
- );
-
- recordGauge(`${METRIC_PREFIX}.pending_projects`, workloads.length, {
- table: tableName,
- });
- recordGauge(`${METRIC_PREFIX}.pending_rows`, totalExpiredRows, {
- table: tableName,
- });
-
logger.info(`${instanceName}: Processing ${workloads.length} projects`, {
projectIds: workloads.map((w) => w.projectId),
- totalExpiredRows,
+ secondsPastCutoff: maxSecondsPastCutoff,
});
// Step 2: Execute single batch DELETE for all selected projects
@@ -207,6 +215,7 @@ export class BatchDataRetentionCleaner {
retentionDays: p.retentionDays!,
cutoffDate: getRetentionCutoffDate(p.retentionDays!, now),
expiredRowCount: 0,
+ oldestAgeSeconds: null,
}),
);
@@ -260,7 +269,10 @@ export class BatchDataRetentionCleaner {
);
const query = `
- SELECT project_id, count() as count
+ SELECT
+ project_id,
+ count() as count,
+ dateDiff('second', min(event_ts), now()) as oldest_age_seconds
FROM ${tableName}
WHERE ${conditions}
GROUP BY project_id
@@ -270,6 +282,7 @@ export class BatchDataRetentionCleaner {
const result = await queryClickhouse<{
project_id: string;
count: number;
+ oldest_age_seconds: number;
}>({
query,
params,
@@ -280,16 +293,26 @@ export class BatchDataRetentionCleaner {
},
});
- // Build a map of counts from the result
- const countMap = new Map(
- result.map((r) => [r.project_id, Number(r.count)]),
+ // Build maps from the result
+ const resultMap = new Map(
+ result.map((r) => [
+ r.project_id,
+ {
+ count: Number(r.count),
+ oldestAgeSeconds: Number(r.oldest_age_seconds),
+ },
+ ]),
);
// Return workloads for all projects (with 0 count if not in result)
- return projects.map((p) => ({
- ...p,
- expiredRowCount: countMap.get(p.projectId) ?? 0,
- }));
+ return projects.map((p) => {
+ const data = resultMap.get(p.projectId);
+ return {
+ ...p,
+ expiredRowCount: data?.count ?? 0,
+ oldestAgeSeconds: data?.oldestAgeSeconds ?? null,
+ };
+ });
}
/**
diff --git a/worker/src/features/media-retention-cleaner/index.ts b/worker/src/features/media-retention-cleaner/index.ts
index 45a302cfb0..9d7fdc1232 100644
--- a/worker/src/features/media-retention-cleaner/index.ts
+++ b/worker/src/features/media-retention-cleaner/index.ts
@@ -17,6 +17,7 @@ interface ProjectWorkload {
retentionDays: number;
cutoffDate: Date;
expiredMediaCount: number;
+ secondsPastCutoff: number | null;
}
/**
@@ -53,10 +54,19 @@ export class MediaRetentionCleaner {
projectId: workload.projectId,
});
+ // Record gauge for how far past cutoff the oldest expired item is
+ if (workload.secondsPastCutoff !== null) {
+ recordGauge(
+ `${METRIC_PREFIX}.seconds_past_cutoff`,
+ Math.max(workload.secondsPastCutoff, 0),
+ );
+ }
+
logger.info(`${instanceName}: Processing project`, {
projectId: workload.projectId,
retentionDays: workload.retentionDays,
expiredMediaCount: workload.expiredMediaCount,
+ secondsPastCutoff: workload.secondsPastCutoff,
});
try {
@@ -88,12 +98,16 @@ export class MediaRetentionCleaner {
project_id: string;
retention_days: number;
expired_count: bigint;
+ seconds_past_cutoff: number;
}>
>`
SELECT
p.id as project_id,
p.retention_days,
- COUNT(m.id) as expired_count
+ COUNT(m.id) as expired_count,
+ EXTRACT(EPOCH FROM (
+ (NOW() - (p.retention_days || ' days')::interval) - MIN(m.created_at)
+ ))::int as seconds_past_cutoff
FROM projects p
INNER JOIN media m ON m.project_id = p.id
WHERE p.retention_days > 0
@@ -114,6 +128,7 @@ export class MediaRetentionCleaner {
retentionDays: row.retention_days,
cutoffDate: getRetentionCutoffDate(row.retention_days, now),
expiredMediaCount: Number(row.expired_count),
+ secondsPastCutoff: row.seconds_past_cutoff,
};
}