Skip to content

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

fix: prevent Mixpanel/PostHog integration jobs from permanently stalling (#11988)

Valery Meleshkin6b87e1bb5f2f
diff --git a/packages/shared/src/server/redis/mixpanelIntegrationQueue.ts b/packages/shared/src/server/redis/mixpanelIntegrationQueue.ts
index 1a546d95cd..59aa1b853f 100644
--- a/packages/shared/src/server/redis/mixpanelIntegrationQueue.ts
+++ b/packages/shared/src/server/redis/mixpanelIntegrationQueue.ts
@@ -7,6 +7,8 @@ import {
} from "./redis";
import { logger } from "../logger";
+export const MIXPANEL_SYNC_CRON_PATTERN = "30 * * * *"; // every hour at :30
+
export class MixpanelIntegrationQueue {
private static instance: Queue | null = null;
@@ -47,7 +49,7 @@ export class MixpanelIntegrationQueue {
QueueJobs.MixpanelIntegrationJob,
{},
{
- repeat: { pattern: "30 * * * *" }, // every hour at 30 minutes past
+ repeat: { pattern: MIXPANEL_SYNC_CRON_PATTERN },
},
)
.catch((err) => {
diff --git a/packages/shared/src/server/redis/postHogIntegrationQueue.ts b/packages/shared/src/server/redis/postHogIntegrationQueue.ts
index 25a459bba3..0891bf7da9 100644
--- a/packages/shared/src/server/redis/postHogIntegrationQueue.ts
+++ b/packages/shared/src/server/redis/postHogIntegrationQueue.ts
@@ -7,6 +7,8 @@ import {
} from "./redis";
import { logger } from "../logger";
+export const POSTHOG_SYNC_CRON_PATTERN = "30 * * * *"; // every hour at :30
+
export class PostHogIntegrationQueue {
private static instance: Queue | null = null;
@@ -47,7 +49,7 @@ export class PostHogIntegrationQueue {
QueueJobs.PostHogIntegrationJob,
{},
{
- repeat: { pattern: "30 * * * *" }, // every hour at 30 minutes past
+ repeat: { pattern: POSTHOG_SYNC_CRON_PATTERN },
},
)
.catch((err) => {
diff --git a/worker/src/app.ts b/worker/src/app.ts
index acaab610b5..2540a2f4d8 100644
--- a/worker/src/app.ts
+++ b/worker/src/app.ts
@@ -447,6 +447,13 @@ if (env.QUEUE_CONSUMER_MIXPANEL_INTEGRATION_QUEUE_IS_ENABLED === "true") {
max: 1,
duration: 10_000,
},
+ // The default lockDuration is 30s and the lockRenewTime 1/2 of that.
+ // We set it to 60s to reduce the number of lock renewals and also be less sensitive to high CPU wait times.
+ // We also update the stalledInterval check to 120s from 30s default to perform the check less frequently.
+ // Finally, we set the maxStalledCount to 3 (default 1) to perform repeated attempts on stalled jobs.
+ lockDuration: 60000, // 60 seconds
+ stalledInterval: 120000, // 120 seconds
+ maxStalledCount: 3,
},
);
}
diff --git a/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts b/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts
index 793bcec2a6..d15d8dc86f 100644
--- a/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts
+++ b/worker/src/features/mixpanel/handleMixpanelIntegrationProjectJob.ts
@@ -32,7 +32,9 @@ const processMixpanelTraces = async (config: MixpanelExecutionConfig) => {
config.maxTimestamp,
);
- logger.info(`Sending traces for project ${config.projectId} to Mixpanel`);
+ logger.info(
+ `[MIXPANEL] Sending traces for project ${config.projectId} to Mixpanel`,
+ );
const mixpanel = new MixpanelClient({
projectToken: config.decryptedMixpanelProjectToken,
@@ -48,13 +50,13 @@ const processMixpanelTraces = async (config: MixpanelExecutionConfig) => {
if (count % 1000 === 0) {
await mixpanel.flush();
logger.info(
- `Sent ${count} traces to Mixpanel for project ${config.projectId}`,
+ `[MIXPANEL] Sent ${count} traces to Mixpanel for project ${config.projectId}`,
);
}
}
await mixpanel.flush();
logger.info(
- `Sent ${count} traces to Mixpanel for project ${config.projectId}`,
+ `[MIXPANEL] Sent ${count} traces to Mixpanel for project ${config.projectId}`,
);
};
@@ -66,7 +68,7 @@ const processMixpanelGenerations = async (config: MixpanelExecutionConfig) => {
);
logger.info(
- `Sending generations for project ${config.projectId} to Mixpanel`,
+ `[MIXPANEL] Sending generations for project ${config.projectId} to Mixpanel`,
);
const mixpanel = new MixpanelClient({
@@ -83,13 +85,13 @@ const processMixpanelGenerations = async (config: MixpanelExecutionConfig) => {
if (count % 1000 === 0) {
await mixpanel.flush();
logger.info(
- `Sent ${count} generations to Mixpanel for project ${config.projectId}`,
+ `[MIXPANEL] Sent ${count} generations to Mixpanel for project ${config.projectId}`,
);
}
}
await mixpanel.flush();
logger.info(
- `Sent ${count} generations to Mixpanel for project ${config.projectId}`,
+ `[MIXPANEL] Sent ${count} generations to Mixpanel for project ${config.projectId}`,
);
};
@@ -100,7 +102,9 @@ const processMixpanelScores = async (config: MixpanelExecutionConfig) => {
config.maxTimestamp,
);
- logger.info(`Sending scores for project ${config.projectId} to Mixpanel`);
+ logger.info(
+ `[MIXPANEL] Sending scores for project ${config.projectId} to Mixpanel`,
+ );
const mixpanel = new MixpanelClient({
projectToken: config.decryptedMixpanelProjectToken,
@@ -116,13 +120,13 @@ const processMixpanelScores = async (config: MixpanelExecutionConfig) => {
if (count % 1000 === 0) {
await mixpanel.flush();
logger.info(
- `Sent ${count} scores to Mixpanel for project ${config.projectId}`,
+ `[MIXPANEL] Sent ${count} scores to Mixpanel for project ${config.projectId}`,
);
}
}
await mixpanel.flush();
logger.info(
- `Sent ${count} scores to Mixpanel for project ${config.projectId}`,
+ `[MIXPANEL] Sent ${count} scores to Mixpanel for project ${config.projectId}`,
);
};
@@ -137,7 +141,9 @@ export const handleMixpanelIntegrationProjectJob = async (
span.setAttribute("messaging.bullmq.job.input.projectId", projectId);
}
- logger.info(`Processing Mixpanel integration for project ${projectId}`);
+ logger.info(
+ `[MIXPANEL] Processing Mixpanel integration for project ${projectId}`,
+ );
// Fetch Mixpanel integration information for project
const mixpanelIntegration = await prisma.mixpanelIntegration.findFirst({
@@ -149,7 +155,7 @@ export const handleMixpanelIntegrationProjectJob = async (
if (!mixpanelIntegration) {
logger.warn(
- `Enabled Mixpanel integration not found for project ${projectId}`,
+ `[MIXPANEL] Enabled Mixpanel integration not found for project ${projectId}`,
);
return;
}
@@ -166,22 +172,30 @@ export const handleMixpanelIntegrationProjectJob = async (
mixpanelRegion: mixpanelIntegration.mixpanelRegion,
};
- await Promise.all([
- processMixpanelTraces(executionConfig),
- processMixpanelGenerations(executionConfig),
- processMixpanelScores(executionConfig),
- ]);
-
- // Update the last run information for the mixpanelIntegration record
- await prisma.mixpanelIntegration.update({
- where: {
- projectId,
- },
- data: {
- lastSyncAt: executionConfig.maxTimestamp,
- },
- });
- logger.info(
- `Mixpanel integration processing complete for project ${projectId}`,
- );
+ try {
+ await Promise.all([
+ processMixpanelTraces(executionConfig),
+ processMixpanelGenerations(executionConfig),
+ processMixpanelScores(executionConfig),
+ ]);
+
+ // Update the last run information for the mixpanelIntegration record.
+ await prisma.mixpanelIntegration.update({
+ where: {
+ projectId,
+ },
+ data: {
+ lastSyncAt: executionConfig.maxTimestamp,
+ },
+ });
+ logger.info(
+ `[MIXPANEL] Mixpanel integration processing complete for project ${projectId}`,
+ );
+ } catch (error) {
+ logger.error(
+ `[MIXPANEL] Error processing Mixpanel integration for project ${projectId}`,
+ error,
+ );
+ throw error;
+ }
};
diff --git a/worker/src/features/mixpanel/handleMixpanelIntegrationSchedule.ts b/worker/src/features/mixpanel/handleMixpanelIntegrationSchedule.ts
index c68e098dac..82c699eda4 100644
--- a/worker/src/features/mixpanel/handleMixpanelIntegrationSchedule.ts
+++ b/worker/src/features/mixpanel/handleMixpanelIntegrationSchedule.ts
@@ -2,6 +2,7 @@ import { prisma } from "@langfuse/shared/src/db";
import {
MixpanelIntegrationProcessingQueue,
QueueJobs,
+ logger,
} from "@langfuse/shared/src/server";
import { randomUUID } from "crypto";
@@ -18,12 +19,26 @@ export const handleMixpanelIntegrationSchedule = async () => {
},
);
+ if (mixpanelIntegrationProjects.length === 0) {
+ logger.info("[MIXPANEL] No Mixpanel integrations ready for sync");
+ return;
+ }
+
const mixpanelIntegrationProcessingQueue =
MixpanelIntegrationProcessingQueue.getInstance();
if (!mixpanelIntegrationProcessingQueue) {
throw new Error("MixpanelIntegrationProcessingQueue not initialized");
}
+ logger.info(
+ `[MIXPANEL] Scheduling ${mixpanelIntegrationProjects.length} Mixpanel integrations for sync`,
+ );
+
+ // Include an hourly key in the jobId so that failed jobs from a previous hour
+ // don't permanently block re-queuing (BullMQ skips adds when a job with the
+ // same ID already exists in a failed state).
+ const hourKey = new Date().toISOString().slice(0, 13); // e.g. "2026-02-11T08"
+
await mixpanelIntegrationProcessingQueue.addBulk(
mixpanelIntegrationProjects.map(
(integration: { projectId: string; lastSyncAt: Date | null }) => ({
@@ -37,8 +52,8 @@ export const handleMixpanelIntegrationSchedule = async () => {
},
},
opts: {
- // Use projectId and last sync as jobId to prevent duplicate jobs.
- jobId: `${integration.projectId}-${integration.lastSyncAt?.toISOString() ?? ""}`,
+ jobId: `${integration.projectId}-${integration.lastSyncAt?.toISOString() ?? ""}-${hourKey}`,
+ removeOnFail: { count: 5 },
},
}),
),
diff --git a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts
index fc489cb35a..1de3c4fd27 100644
--- a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts
+++ b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts
@@ -37,7 +37,9 @@ const processPostHogTraces = async (config: PostHogExecutionConfig) => {
config.maxTimestamp,
);
- logger.info(`Sending traces for project ${config.projectId} to PostHog`);
+ logger.info(
+ `[POSTHOG] Sending traces for project ${config.projectId} to PostHog`,
+ );
// Send each via PostHog SDK
const posthog = new PostHog(config.decryptedPostHogApiKey, {
@@ -47,10 +49,10 @@ const processPostHogTraces = async (config: PostHogExecutionConfig) => {
posthog.on("error", (error) => {
logger.error(
- `Error sending traces to PostHog for project ${config.projectId}: ${error}`,
+ `[POSTHOG] Error sending traces to PostHog for project ${config.projectId}: ${error}`,
);
throw new Error(
- `Error sending traces to PostHog for project ${config.projectId}: ${error}`,
+ `[POSTHOG] Error sending traces to PostHog for project ${config.projectId}: ${error}`,
);
});
@@ -62,13 +64,13 @@ const processPostHogTraces = async (config: PostHogExecutionConfig) => {
if (count % 10000 === 0) {
await posthog.flush();
logger.info(
- `Sent ${count} traces to PostHog for project ${config.projectId}`,
+ `[POSTHOG] Sent ${count} traces to PostHog for project ${config.projectId}`,
);
}
}
await posthog.flush();
logger.info(
- `Sent ${count} traces to PostHog for project ${config.projectId}`,
+ `[POSTHOG] Sent ${count} traces to PostHog for project ${config.projectId}`,
);
};
@@ -79,7 +81,9 @@ const processPostHogGenerations = async (config: PostHogExecutionConfig) => {
config.maxTimestamp,
);
- logger.info(`Sending generations for project ${config.projectId} to PostHog`);
+ logger.info(
+ `[POSTHOG] Sending generations for project ${config.projectId} to PostHog`,
+ );
// Send each via PostHog SDK
const posthog = new PostHog(config.decryptedPostHogApiKey, {
@@ -89,10 +93,10 @@ const processPostHogGenerations = async (config: PostHogExecutionConfig) => {
posthog.on("error", (error) => {
logger.error(
- `Error sending generations to PostHog for project ${config.projectId}: ${error}`,
+ `[POSTHOG] Error sending generations to PostHog for project ${config.projectId}: ${error}`,
);
throw new Error(
- `Error sending generations to PostHog for project ${config.projectId}: ${error}`,
+ `[POSTHOG] Error sending generations to PostHog for project ${config.projectId}: ${error}`,
);
});
@@ -104,13 +108,13 @@ const processPostHogGenerations = async (config: PostHogExecutionConfig) => {
if (count % 10000 === 0) {
await posthog.flush();
logger.info(
- `Sent ${count} generations to PostHog for project ${config.projectId}`,
+ `[POSTHOG] Sent ${count} generations to PostHog for project ${config.projectId}`,
);
}
}
await posthog.flush();
logger.info(
- `Sent ${count} generations to PostHog for project ${config.projectId}`,
+ `[POSTHOG] Sent ${count} generations to PostHog for project ${config.projectId}`,
);
};
@@ -121,7 +125,9 @@ const processPostHogScores = async (config: PostHogExecutionConfig) => {
config.maxTimestamp,
);
- logger.info(`Sending scores for project ${config.projectId} to PostHog`);
+ logger.info(
+ `[POSTHOG] Sending scores for project ${config.projectId} to PostHog`,
+ );
// Send each via PostHog SDK
const posthog = new PostHog(config.decryptedPostHogApiKey, {
@@ -131,10 +137,10 @@ const processPostHogScores = async (config: PostHogExecutionConfig) => {
posthog.on("error", (error) => {
logger.error(
- `Error sending scores to PostHog for project ${config.projectId}: ${error}`,
+ `[POSTHOG] Error sending scores to PostHog for project ${config.projectId}: ${error}`,
);
throw new Error(
- `Error sending scores to PostHog for project ${config.projectId}: ${error}`,
+ `[POSTHOG] Error sending scores to PostHog for project ${config.projectId}: ${error}`,
);
});
let count = 0;
@@ -145,13 +151,13 @@ const processPostHogScores = async (config: PostHogExecutionConfig) => {
if (count % 10000 === 0) {
await posthog.flush();
logger.info(
- `Sent ${count} scores to PostHog for project ${config.projectId}`,
+ `[POSTHOG] Sent ${count} scores to PostHog for project ${config.projectId}`,
);
}
}
await posthog.flush();
logger.info(
- `Sent ${count} scores to PostHog for project ${config.projectId}`,
+ `[POSTHOG] Sent ${count} scores to PostHog for project ${config.projectId}`,
);
};
@@ -166,7 +172,9 @@ export const handlePostHogIntegrationProjectJob = async (
span.setAttribute("messaging.bullmq.job.input.projectId", projectId);
}
- logger.info(`Processing PostHog integration for project ${projectId}`);
+ logger.info(
+ `[POSTHOG] Processing PostHog integration for project ${projectId}`,
+ );
// Fetch PostHog integration information for project
const postHogIntegration = await prisma.posthogIntegration.findFirst({
@@ -178,7 +186,7 @@ export const handlePostHogIntegrationProjectJob = async (
if (!postHogIntegration) {
logger.warn(
- `Enabled PostHog integration not found for project ${projectId}`,
+ `[POSTHOG] Enabled PostHog integration not found for project ${projectId}`,
);
return;
}
@@ -188,7 +196,7 @@ export const handlePostHogIntegrationProjectJob = async (
await validateWebhookURL(postHogIntegration.posthogHostName);
} catch (error) {
logger.error(
- `PostHog integration for project ${projectId} has invalid hostname: ${postHogIntegration.posthogHostName}. Error: ${error instanceof Error ? error.message : String(error)}`,
+ `[POSTHOG] PostHog integration for project ${projectId} has invalid hostname: ${postHogIntegration.posthogHostName}. Error: ${error instanceof Error ? error.message : String(error)}`,
);
throw new Error(
`Invalid PostHog hostname for project ${projectId}: ${error instanceof Error ? error.message : "Unknown error"}`,
@@ -205,22 +213,30 @@ export const handlePostHogIntegrationProjectJob = async (
postHogHost: postHogIntegration.posthogHostName,
};
- await Promise.all([
- processPostHogTraces(executionConfig),
- processPostHogGenerations(executionConfig),
- processPostHogScores(executionConfig),
- ]);
-
- // Update the last run information for the postHogIntegration record
- await prisma.posthogIntegration.update({
- where: {
- projectId,
- },
- data: {
- lastSyncAt: executionConfig.maxTimestamp,
- },
- });
- logger.info(
- `PostHog integration processing complete for project ${projectId}`,
- );
+ try {
+ await Promise.all([
+ processPostHogTraces(executionConfig),
+ processPostHogGenerations(executionConfig),
+ processPostHogScores(executionConfig),
+ ]);
+
+ // Update the last run information for the postHogIntegration record.
+ await prisma.posthogIntegration.update({
+ where: {
+ projectId,
+ },
+ data: {
+ lastSyncAt: executionConfig.maxTimestamp,
+ },
+ });
+ logger.info(
+ `[POSTHOG] PostHog integration processing complete for project ${projectId}`,
+ );
+ } catch (error) {
+ logger.error(
+ `[POSTHOG] Error processing PostHog integration for project ${projectId}`,
+ error,
+ );
+ throw error;
+ }
};
diff --git a/worker/src/features/posthog/handlePostHogIntegrationSchedule.ts b/worker/src/features/posthog/handlePostHogIntegrationSchedule.ts
index 850a3f7475..363c6c47ca 100644
--- a/worker/src/features/posthog/handlePostHogIntegrationSchedule.ts
+++ b/worker/src/features/posthog/handlePostHogIntegrationSchedule.ts
@@ -2,6 +2,7 @@ import { prisma } from "@langfuse/shared/src/db";
import {
PostHogIntegrationProcessingQueue,
QueueJobs,
+ logger,
} from "@langfuse/shared/src/server";
import { randomUUID } from "crypto";
@@ -16,12 +17,26 @@ export const handlePostHogIntegrationSchedule = async () => {
},
});
+ if (postHogIntegrationProjects.length === 0) {
+ logger.info("[POSTHOG] No PostHog integrations ready for sync");
+ return;
+ }
+
const postHogIntegrationProcessingQueue =
PostHogIntegrationProcessingQueue.getInstance();
if (!postHogIntegrationProcessingQueue) {
throw new Error("PostHogIntegrationProcessingQueue not initialized");
}
+ logger.info(
+ `[POSTHOG] Scheduling ${postHogIntegrationProjects.length} PostHog integrations for sync`,
+ );
+
+ // Include an hourly key in the jobId so that failed jobs from a previous hour
+ // don't permanently block re-queuing (BullMQ skips adds when a job with the
+ // same ID already exists in a failed state).
+ const hourKey = new Date().toISOString().slice(0, 13); // e.g. "2026-02-11T08"
+
await postHogIntegrationProcessingQueue.addBulk(
postHogIntegrationProjects.map((integration) => ({
name: QueueJobs.PostHogIntegrationProcessingJob,
@@ -34,8 +49,8 @@ export const handlePostHogIntegrationSchedule = async () => {
},
},
opts: {
- // Use projectId and last sync as jobId to prevent duplicate jobs.
- jobId: `${integration.projectId}-${integration.lastSyncAt?.toISOString() ?? ""}`,
+ jobId: `${integration.projectId}-${integration.lastSyncAt?.toISOString() ?? ""}-${hourKey}`,
+ removeOnFail: { count: 5 },
},
})),
);