Skip to content

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

wrap inside transaction

Thiago Araujo da Silva6ae3dc66aff8
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 5c197b71c3..281a98b5b4 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
@@ -146,6 +146,27 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
}
};
+ const applyBatchFailure = async (input: {
+ retriable: { ids: number[]; nextRetryAt: Date } | null;
+ exhausted: TAuditLogStreamOutboxRow[];
+ errorMessage: string;
+ }) => {
+ const retriableIds = input.retriable?.ids ?? [];
+ if (retriableIds.length === 0 && input.exhausted.length === 0) return;
+ try {
+ await db.transaction(async (tx) => {
+ if (input.retriable && retriableIds.length > 0) {
+ await markBatchForRetry(input.retriable.ids, input.retriable.nextRetryAt, input.errorMessage, tx);
+ }
+ if (input.exhausted.length > 0) {
+ await moveToDlq(input.exhausted, input.errorMessage, tx);
+ }
+ });
+ } catch (error) {
+ throw new DatabaseError({ error, name: "AuditLogStreamOutbox: applyBatchFailure" });
+ }
+ };
+
// Stale-claim recovery: rows whose worker crashed mid-batch never had their
// 'processing' status flipped back, so they're invisible to the drain query.
// Treat a stale claim as a consumed attempt — a payload that crashes the worker
@@ -209,8 +230,7 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
batchInsert,
claimBatchForStream,
deleteByIds,
- markBatchForRetry,
- moveToDlq,
+ applyBatchFailure,
recoverStaleClaims
};
};
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
index f84d66b937..498dfbe009 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.test.ts
@@ -58,8 +58,7 @@ const createService = () => {
batchInsert: vi.fn(async () => undefined),
claimBatchForStream: vi.fn<(...args: unknown[]) => Promise<TAuditLogStreamOutboxRow[]>>(async () => []),
deleteByIds: vi.fn(async () => undefined),
- markBatchForRetry: vi.fn(async () => undefined),
- moveToDlq: vi.fn(async () => undefined),
+ applyBatchFailure: vi.fn(async () => undefined),
recoverStaleClaims: vi.fn(async () => ({ retried: 0, movedToDlq: 0 }))
};
@@ -106,8 +105,11 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- asymmetric matcher is typed `any`
errorMessage: expect.stringContaining(FAILURE_MESSAGE)
});
- expect(auditLogStreamOutboxDAL.markBatchForRetry).toHaveBeenCalledTimes(1);
- expect(auditLogStreamOutboxDAL.moveToDlq).not.toHaveBeenCalled();
+ expect(auditLogStreamOutboxDAL.applyBatchFailure).toHaveBeenCalledTimes(1);
+ expect(auditLogStreamOutboxDAL.applyBatchFailure.mock.calls[0][0]).toMatchObject({
+ retriable: { ids: [1] },
+ exhausted: []
+ });
expect(auditLogStreamOutboxDAL.deleteByIds).not.toHaveBeenCalled();
});
@@ -123,8 +125,13 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
await service.drainStream({ streamId: STREAM_ID, orgId: ORG_ID, provider: PROVIDER });
expect(onStreamFailure).toHaveBeenCalledTimes(1);
- expect(auditLogStreamOutboxDAL.moveToDlq).toHaveBeenCalledTimes(1);
- expect(auditLogStreamOutboxDAL.markBatchForRetry).not.toHaveBeenCalled();
+ expect(auditLogStreamOutboxDAL.applyBatchFailure).toHaveBeenCalledTimes(1);
+ const call = auditLogStreamOutboxDAL.applyBatchFailure.mock.calls[0][0] as {
+ retriable: unknown;
+ exhausted: TAuditLogStreamOutboxRow[];
+ };
+ expect(call.retriable).toBeNull();
+ expect(call.exhausted).toHaveLength(1);
});
test("does not invoke onStreamFailure on the success path", async () => {
@@ -137,8 +144,7 @@ describe("audit-log-stream-outbox-service drainStream failure wiring", () => {
expect(onStreamFailure).not.toHaveBeenCalled();
expect(auditLogStreamOutboxDAL.deleteByIds).toHaveBeenCalledTimes(1);
- expect(auditLogStreamOutboxDAL.markBatchForRetry).not.toHaveBeenCalled();
- expect(auditLogStreamOutboxDAL.moveToDlq).not.toHaveBeenCalled();
+ expect(auditLogStreamOutboxDAL.applyBatchFailure).not.toHaveBeenCalled();
});
test("stops draining after the first failed batch — does not claim another batch", async () => {
@@ -159,8 +165,7 @@ describe("audit-log-stream-outbox-service enqueueForLogs batch fanout", () => {
batchInsert: vi.fn<(rows: unknown[]) => Promise<void>>(async () => undefined),
claimBatchForStream: vi.fn(async () => []),
deleteByIds: vi.fn(async () => undefined),
- markBatchForRetry: vi.fn(async () => undefined),
- moveToDlq: vi.fn(async () => undefined),
+ applyBatchFailure: vi.fn(async () => undefined),
recoverStaleClaims: vi.fn(async () => ({ retried: 0, movedToDlq: 0 }))
};
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 d4273ee6fb..bb6df87858 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
@@ -20,7 +20,7 @@ const FLUSH_DEBOUNCE_MS = 5_000;
const FLUSH_DEBOUNCE_SECONDS = Math.ceil(FLUSH_DEBOUNCE_MS / 1_000);
// Worker drain settings.
-const BATCH_SIZE = 1_000;
+const BATCH_SIZE = 500;
const MAX_BATCHES_PER_JOB = 10; // hard cap so one job can't monopolize the worker
const MAX_ATTEMPTS = 5;
// Exponential backoff with jitter, capped — keeps a wedged provider from hot-looping.
@@ -219,7 +219,6 @@ export const auditLogStreamOutboxServiceFactory = ({
const exhausted: TAuditLogStreamOutboxRow[] = [];
const retriable: TAuditLogStreamOutboxRow[] = [];
- // attempts on the row is pre-increment. The +1 reflects this failure.
for (const row of claimed) {
if (row.attempts + 1 >= MAX_ATTEMPTS) {
exhausted.push(row);
@@ -228,24 +227,18 @@ export const auditLogStreamOutboxServiceFactory = ({
}
}
- if (retriable.length > 0) {
- // Use the same nextRetryAt for the whole retriable batch — they share
- // a failure cause, so they should re-enter together. Each row's own
- // attempts count is incremented atomically in DAL.
- const maxAttemptsInBatch = Math.max(...retriable.map((row) => row.attempts + 1));
- const nextRetryAt = computeBackoff(maxAttemptsInBatch);
- // eslint-disable-next-line no-await-in-loop
- await auditLogStreamOutboxDAL.markBatchForRetry(
- retriable.map((row) => row.id),
- nextRetryAt,
- errorMessage
- );
- }
-
- if (exhausted.length > 0) {
- // eslint-disable-next-line no-await-in-loop
- await auditLogStreamOutboxDAL.moveToDlq(exhausted, errorMessage);
- }
+ const retriableInput = retriable.length
+ ? {
+ ids: retriable.map((row) => row.id),
+ nextRetryAt: computeBackoff(Math.max(...retriable.map((row) => row.attempts + 1)))
+ }
+ : null;
+ // eslint-disable-next-line no-await-in-loop
+ await auditLogStreamOutboxDAL.applyBatchFailure({
+ retriable: retriableInput,
+ exhausted,
+ errorMessage
+ });
if (onStreamFailure) {
// eslint-disable-next-line no-await-in-loop
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 2f32ab58a1..597369a958 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -451,7 +451,7 @@ export const keyStoreFactory = (
const listLength = async (key: string) => pickPrimaryOrSecondaryRedis(primaryRedis, redisReadReplicas).llen(key);
// Stream operations
- const streamAdd = async (key: string, id: string, fieldValue: Record<string, string>, maxLen = 1000000) => {
+ const streamAdd = async (key: string, id: string, fieldValue: Record<string, string>, maxLen = 1_000_000) => {
const args: string[] = [];
for (const [field, value] of Object.entries(fieldValue)) {
args.push(field, value);