Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

woke up stale claims on sweep job

Thiago Araujo da Silvad68cc3c94bbb
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
index 76bdded6c7..00d39e497f 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
@@ -267,10 +267,25 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
}
};
+ const findStreamsWithOverdueRows = async (): Promise<{ streamId: string; orgId: string; provider: string }[]> => {
+ try {
+ return await db(`${TableName.AuditLogStreamOutbox} as o`)
+ .join(`${TableName.AuditLogStream} as s`, "s.id", "o.streamId")
+ .whereIn("o.status", [AuditLogStreamOutboxStatus.Pending, AuditLogStreamOutboxStatus.Retry])
+ .andWhere("o.nextRetryAt", "<=", db.fn.now())
+ .distinct<
+ { streamId: string; orgId: string; provider: string }[]
+ >("o.streamId as streamId", "o.orgId as orgId", "s.provider as provider");
+ } catch (error) {
+ throw new DatabaseError({ error, name: "AuditLogStreamOutbox: findStreamsWithOverdueRows" });
+ }
+ };
+
return {
batchInsert,
claimBatchForStream,
commitDeliveryResult,
- recoverStaleClaims
+ recoverStaleClaims,
+ findStreamsWithOverdueRows
};
};
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
index dd67765ec0..e42463fc8b 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
@@ -268,9 +268,23 @@ export const auditLogStreamOutboxServiceFactory = ({
}
};
- // Stale-claim sweeper. Rows whose worker crashed mid-batch stay 'processing'
- // forever and never re-enter the drain query. Flip them back to 'retry' so
- // the next event for that stream picks them up via the normal debounce path.
+ // Stale-claim sweeper. Two responsibilities, both about preventing rows from
+ // getting stuck in the outbox:
+ // 1. Rows whose worker crashed mid-batch stay 'processing' forever and never
+ // re-enter the drain query — flip them back to 'retry' (or DLQ if attempts
+ // are exhausted).
+ // 2. Rows in 'pending'/'retry' whose nextRetryAt is now in the past but never
+ // got a follow-up flush job (drainStream bailed on a chunk failure and no
+ // new event arrived after backoff). For these, enqueue a flush via the
+ // normal debounce path.
+ //
+ // The wake-up step does not touch row state — it only enqueues a flush. Duplicate
+ // deliveries are still prevented by:
+ // - BullMQ jobId dedup ('flush:<streamId>') if a real flush is already
+ // delayed/active for the stream;
+ // - the SETNX debounce key inside debounceAndEnqueueFlush;
+ // - claimBatchForStream's FOR UPDATE SKIP LOCKED + atomic flip to 'processing'
+ // so even concurrent workers claim disjoint rows.
const sweepStaleClaims = async () => {
const { retried, movedToDlq } = await auditLogStreamOutboxDAL.recoverStaleClaims(
STALE_CLAIM_THRESHOLD_MS,
@@ -281,6 +295,17 @@ export const auditLogStreamOutboxServiceFactory = ({
`audit-log-stream-outbox: recovered stale claims [retried=${retried}] [movedToDlq=${movedToDlq}] [thresholdMs=${STALE_CLAIM_THRESHOLD_MS}]`
);
}
+
+ const overdueStreams = await auditLogStreamOutboxDAL.findStreamsWithOverdueRows();
+ if (overdueStreams.length === 0) return;
+
+ const results = await Promise.allSettled(
+ overdueStreams.map(({ streamId, orgId, provider }) =>
+ debounceAndEnqueueFlush(streamId, orgId, provider as LogProvider)
+ )
+ );
+ const woken = results.filter((r) => r.status === "fulfilled" && r.value === true).length;
+ logger.info(`audit-log-stream-outbox: swept overdue streams [overdue=${overdueStreams.length}] [woken=${woken}]`);
};
return {