Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
rename from row to event on consumer
Thiago Araujo da Silva8a81e81fcf4a
diff --git a/backend/src/services/alert/alert-event-consumer.test.ts b/backend/src/services/alert/alert-event-consumer.test.tsindex 9a79b565f3..9863e9173c 100644--- a/backend/src/services/alert/alert-event-consumer.test.ts+++ b/backend/src/services/alert/alert-event-consumer.test.ts@@ -15,7 +15,7 @@ const EVENT_TYPE = "approval.workflow.request_opened";const makeAlert = (id: string) => ({ id, resourceType: "approval.workflow", orgId: ORG_ID }) as never;-const makeRow = (overrides?: Partial<TEventOutbox>): TEventOutbox =>+const makeEvent = (overrides?: Partial<TEventOutbox>): TEventOutbox =>({id: 1,consumer: "alert",@@ -92,10 +92,10 @@ describe("alert outbox consumer", () => {expect(consumer.subscribesTo("identity.authentication.expiry")).toBe(false);});- test("delivers the event's targets and marks the row delivered", async () => {+ test("delivers the event's targets and marks it delivered", async () => {const { consumer, runs } = buildConsumer();- const [result] = await consumer.handle([makeRow()]);+ const [result] = await consumer.handle([makeEvent()]);expect(result.status).toBe(EventOutboxStatus.Delivered);expect(runs[0].targetIds).toEqual(["req-1"]);@@ -106,10 +106,10 @@ describe("alert outbox consumer", () => {// The alert was deleted or disabled between the gate and the worker, so nobody is owed a// notification. That's a completed event, not something to retry forever.- test("marks the row delivered when no alert matches any more", async () => {+ test("marks the event delivered when no alert matches any more", async () => {const { consumer, runs } = buildConsumer({ alerts: [] });- const [result] = await consumer.handle([makeRow()]);+ const [result] = await consumer.handle([makeEvent()]);expect(result.status).toBe(EventOutboxStatus.Delivered);expect(runs).toHaveLength(0);@@ -120,7 +120,7 @@ describe("alert outbox consumer", () => {results: [{ outcome: AlertDispatchOutcome.DeliveryPartial, deliveredChannelIds: ["c-1"] }]});- const [result] = await consumer.handle([makeRow()]);+ const [result] = await consumer.handle([makeEvent()]);expect(result.status).toBe(EventOutboxStatus.Retry);expect(result.progress).toEqual({ deliveredChannelIds: ["c-1"] });@@ -129,7 +129,7 @@ describe("alert outbox consumer", () => {test("skips the channels a previous attempt already delivered to", async () => {const { consumer, runs } = buildConsumer();- await consumer.handle([makeRow({ progress: { deliveredChannelIds: ["c-1"] } })]);+ await consumer.handle([makeEvent({ progress: { deliveredChannelIds: ["c-1"] } })]);expect(runs[0].skipChannelIds).toEqual(["c-1"]);});@@ -138,7 +138,7 @@ describe("alert outbox consumer", () => {test("fails terminally on an unreadable payload", async () => {const { consumer } = buildConsumer();- const [result] = await consumer.handle([makeRow({ payload: { targetIds: [] } })]);+ const [result] = await consumer.handle([makeEvent({ payload: { targetIds: [] } })]);expect(result.status).toBe(EventOutboxStatus.Failed);expect(result.error).toContain("Unreadable alert event payload");@@ -153,7 +153,7 @@ describe("alert outbox consumer", () => {]});- const [result] = await consumer.handle([makeRow()]);+ const [result] = await consumer.handle([makeEvent()]);expect(runs.map((run) => run.alertId)).toEqual(["alert-project", "alert-org"]);expect(result.progress).toEqual({ deliveredChannelIds: ["c-1", "c-2"] });@@ -164,7 +164,7 @@ describe("alert outbox consumer", () => {test("fails terminally when no provider declares the event for that resource type", async () => {const { consumer, runs, getLookups } = buildConsumer();- const [result] = await consumer.handle([makeRow({ resourceType: "pki.certificate" })]);+ const [result] = await consumer.handle([makeEvent({ resourceType: "pki.certificate" })]);expect(result.status).toBe(EventOutboxStatus.Failed);expect(result.error).toContain("pki.certificate");@@ -173,9 +173,9 @@ describe("alert outbox consumer", () => {expect(getLookups()).toBe(0);});- // Throwing out of handle() sends the whole batch back for retry, re-notifying on rows that already- // delivered, so one row's failure has to stay that row's failure.- test("retries only the row whose lookup threw", async () => {+ // Throwing out of handle() sends the whole batch back for retry, re-notifying on events that already+ // delivered, so one event's failure has to stay that event's failure.+ test("retries only the event whose lookup threw", async () => {let calls = 0;const { consumer, runs } = buildConsumer({eventKeys: [EVENT_TYPE, "approval.workflow.request_closed", "approval.workflow.request_reopened"],@@ -187,9 +187,9 @@ describe("alert outbox consumer", () => {});const results = await consumer.handle([- makeRow({ id: 1, eventType: EVENT_TYPE }),- makeRow({ id: 2, eventType: "approval.workflow.request_closed" }),- makeRow({ id: 3, eventType: "approval.workflow.request_reopened" })+ makeEvent({ id: 1, eventType: EVENT_TYPE }),+ makeEvent({ id: 2, eventType: "approval.workflow.request_closed" }),+ makeEvent({ id: 3, eventType: "approval.workflow.request_reopened" })]);expect(results.map((result) => result.status)).toEqual([@@ -205,22 +205,22 @@ describe("alert outbox consumer", () => {const { consumer, getLookups } = buildConsumer();await consumer.handle([- makeRow({ id: 1, payload: { targetIds: ["req-1"] } }),- makeRow({ id: 2, payload: { targetIds: ["req-2"] } }),- makeRow({ id: 3, payload: { targetIds: ["req-3"] } })+ makeEvent({ id: 1, payload: { targetIds: ["req-1"] } }),+ makeEvent({ id: 2, payload: { targetIds: ["req-2"] } }),+ makeEvent({ id: 3, payload: { targetIds: ["req-3"] } })]);expect(getLookups()).toBe(1);});- // Rows arrive in id order and share one resource, so handling them concurrently would deliver that+ // Events arrive in id order and share one resource, so handling them concurrently would deliver that// resource's events out of order.- test("handles rows serially, in the order given", async () => {+ test("handles events serially, in the order given", async () => {const { consumer, runs } = buildConsumer();const results = await consumer.handle([- makeRow({ id: 1, payload: { targetIds: ["req-1"] } }),- makeRow({ id: 2, payload: { targetIds: ["req-2"] } })+ makeEvent({ id: 1, payload: { targetIds: ["req-1"] } }),+ makeEvent({ id: 2, payload: { targetIds: ["req-2"] } })]);expect(results.map((result) => result.id)).toEqual(["1", "2"]);diff --git a/backend/src/services/alert/alert-event-consumer.ts b/backend/src/services/alert/alert-event-consumer.tsindex 853d76d4ba..44bc27bf6a 100644--- a/backend/src/services/alert/alert-event-consumer.ts+++ b/backend/src/services/alert/alert-event-consumer.ts@@ -5,7 +5,7 @@ import { AlertDispatchOutcome, recordAlertDispatchOutcomeMetric } from "@app/libimport {EventOutboxStatus,IEventOutboxConsumer,- TOutboxRowResult+ TOutboxEventResult} from "@app/services/event-outbox/event-outbox-types";import { TAlertDALFactory } from "./alert-dal";@@ -41,26 +41,26 @@ export const alertEventConsumerFactory = ({const subscribesTo = (eventType: string): boolean => alertProviderRegistry.eventTriggeredKeys().has(eventType);// subscribesTo only sees the event type, so a valid event key on the wrong resourceType gets past it.- // Fail here, terminally and naming both, instead of marking the row delivered as "no matching alert".- const $isDeclaredEvent = (row: TEventOutbox): boolean =>+ // Fail here, terminally and naming both, instead of marking the event delivered as "no matching alert".+ const $isDeclaredEvent = (event: TEventOutbox): boolean =>Boolean(alertProviderRegistry- .get(row.resourceType)- ?.events.some((event) => event.key === row.eventType && event.triggerType === AlertTriggerType.Event)+ .get(event.resourceType)+ ?.events.some((declared) => declared.key === event.eventType && declared.triggerType === AlertTriggerType.Event));- const $handleRow = async (row: TEventOutbox, findAlerts: TAlertLookup): Promise<TOutboxRowResult> => {- const id = String(row.id);+ const $handleEvent = async (event: TEventOutbox, findAlerts: TAlertLookup): Promise<TOutboxEventResult> => {+ const id = String(event.id);- if (!$isDeclaredEvent(row)) {+ if (!$isDeclaredEvent(event)) {return {id,status: EventOutboxStatus.Failed,- error: `No alert provider for resource type '${row.resourceType}' declares '${row.eventType}' as an event-triggered alert`+ error: `No alert provider for resource type '${event.resourceType}' declares '${event.eventType}' as an event-triggered alert`};}- const payload = AlertEventPayloadSchema.safeParse(row.payload);+ const payload = AlertEventPayloadSchema.safeParse(event.payload);if (!payload.success) {return {id,@@ -69,20 +69,20 @@ export const alertEventConsumerFactory = ({};}- const progress = AlertEventProgressSchema.safeParse(row.progress ?? {});+ const progress = AlertEventProgressSchema.safeParse(event.progress ?? {});const delivered = new Set(progress.success ? progress.data.deliveredChannelIds : []);const alerts = await findAlerts({- orgId: row.orgId,- projectId: row.projectId,- resourceType: row.resourceType,- resourceId: row.resourceId,- eventType: row.eventType+ orgId: event.orgId,+ projectId: event.projectId,+ resourceType: event.resourceType,+ resourceId: event.resourceId,+ eventType: event.eventType});if (alerts.length === 0) {recordAlertDispatchOutcomeMetric({- resourceType: row.resourceType,+ resourceType: event.resourceType,outcome: AlertDispatchOutcome.NoMatchingAlert});return { id, status: EventOutboxStatus.Delivered };@@ -93,9 +93,9 @@ export const alertEventConsumerFactory = ({for (const alert of alerts) {// eslint-disable-next-line no-await-in-loop -- at most a project- and an org-scoped alertconst result = await alertEngine.runAlertForEvent(alert, {- eventType: row.eventType,+ eventType: event.eventType,targetIds: payload.data.targetIds,- payload: (row.payload ?? {}) as Record<string, unknown>,+ payload: (event.payload ?? {}) as Record<string, unknown>,skipChannelIds: [...delivered]});result.deliveredChannelIds.forEach((channelId) => delivered.add(channelId));@@ -116,7 +116,7 @@ export const alertEventConsumerFactory = ({return { ...outcome, status: EventOutboxStatus.Delivered };};- const handle = async (rows: TEventOutbox[]): Promise<TOutboxRowResult[]> => {+ const handle = async (events: TEventOutbox[]): Promise<TOutboxEventResult[]> => {const alertsByScope = new Map<string, ReturnType<TAlertLookup>>();const findAlerts: TAlertLookup = (filter) => {const key = `${filter.orgId}|${filter.projectId ?? ""}|${filter.eventType}`;@@ -128,15 +128,15 @@ export const alertEventConsumerFactory = ({return lookup;};- const results: TOutboxRowResult[] = [];+ const results: TOutboxEventResult[] = [];- for (const row of rows) {+ for (const event of events) {try {// eslint-disable-next-line no-await-in-loop -- ordering is the point- results.push(await $handleRow(row, findAlerts));+ results.push(await $handleEvent(event, findAlerts));} catch (error) {results.push({- id: String(row.id),+ id: String(event.id),status: EventOutboxStatus.Retry,error: error instanceof Error ? error.message : "Unknown error"});diff --git a/backend/src/services/event-outbox/event-outbox-service.ts b/backend/src/services/event-outbox/event-outbox-service.tsindex 12d3058b02..1b97079b5f 100644--- a/backend/src/services/event-outbox/event-outbox-service.ts+++ b/backend/src/services/event-outbox/event-outbox-service.ts@@ -20,8 +20,8 @@ import {OutboxEventSchema,STALE_CLAIM_THRESHOLD_MS,TOutboxEvent,- TOutboxFlushKey,- TOutboxRowResult+ TOutboxEventResult,+ TOutboxFlushKey} from "./event-outbox-types";const PRUNE_BATCH_SIZE = 5_000;@@ -142,7 +142,11 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry}};- const $applyResults = async (rows: TEventOutbox[], lockToken: string, results: TOutboxRowResult[]): Promise<void> => {+ const $applyResults = async (+ rows: TEventOutbox[],+ lockToken: string,+ results: TOutboxEventResult[]+ ): Promise<void> => {const byId = new Map(results.map((result) => [result.id, result]));const now = Date.now();@@ -166,7 +170,7 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistry};const attempts = row.attempts + 1;- let finalStatus: TOutboxRowResult["status"];+ let finalStatus: TOutboxEventResult["status"];if (result.status === EventOutboxStatus.Delivered) {finalStatus = EventOutboxStatus.Delivered;delivered.push({ id, progress: result.progress });@@ -222,7 +226,7 @@ export const eventOutboxServiceFactory = ({ eventOutboxDAL, eventOutboxRegistrykey: TOutboxFlushKey,claimed: TEventOutbox[],lockToken: string- ): Promise<TOutboxRowResult[]> => {+ ): Promise<TOutboxEventResult[]> => {const ids = claimed.map((row) => String(row.id));const heartbeat = setInterval(() => {eventOutboxDAL.extendClaims(ids, lockToken).catch((error) => {diff --git a/backend/src/services/event-outbox/event-outbox-types.ts b/backend/src/services/event-outbox/event-outbox-types.tsindex 918980f6a7..f5bcef85af 100644--- a/backend/src/services/event-outbox/event-outbox-types.ts+++ b/backend/src/services/event-outbox/event-outbox-types.ts@@ -62,7 +62,7 @@ export const OutboxEventSchema = z.object({export type TOutboxEvent = z.input<typeof OutboxEventSchema>;-export type TOutboxRowResult = {+export type TOutboxEventResult = {id: string;status: EventOutboxStatus.Delivered | EventOutboxStatus.Retry | EventOutboxStatus.Failed;error?: string;@@ -88,6 +88,6 @@ export interface IEventOutboxConsumer<TPayload = unknown> {// wants the event is decided later, in handle().subscribesTo(eventType: string): boolean;- // Rows for a single (resourceType, resourceId), in id order. Must return one result per row.- handle(rows: TEventOutbox[]): Promise<TOutboxRowResult[]>;+ // Events for a single (resourceType, resourceId), in id order. Must return one result per event.+ handle(events: TEventOutbox[]): Promise<TOutboxEventResult[]>;}