feat(webhooks): dispatch on emit instead of waiting for the next cron tick (#1256)
* feat(webhooks): dispatch on emit instead of waiting for the next cron tick The webhook dispatcher ran only on a per-minute cron, so the floor on delivery latency was up to 60 seconds plus the request. An external consumer that wanted to react as a transaction landed had only one alternative: polling /api/events, which the 100 rpm per-key limit makes expensive and which still cannot beat the tick interval. Schedules one dispatch cycle as soon as deliveries are enqueued. The cron is unchanged and remains the retry and sweep path; this only moves the first attempt forward. Wired into the event-bus fanout plus the two routes that enqueue a delivery directly: the :test verb, whose entire purpose is telling someone whether their receiver works, and the manual delivery retry. Three properties are load-bearing and covered by tests. The kick is never awaited, because eventBus.emit is awaited at ~99 call sites including journal_entry.committed and each delivery can burn a 10 s receiver timeout. It coalesces per function instance, so a bulk booking that emits once per row does not schedule one claim round trip per row. It claims 5 rows rather than the cron's 50, because it runs on the tail of a user-facing request. Double delivery is not a risk: claim_due_webhook_deliveries already claims FOR UPDATE SKIP LOCKED and flips rows to in_flight in the same statement, so a kick racing the cron sees disjoint rows. Does not close #1201, which asks for a realtime stream for API consumers. This is the cheap half. Refs #1201 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(webhooks): stop claiming the kick makes double delivery impossible Adversarial review of the previous commit caught an overstatement in its own comments. SKIP LOCKED keeps a kick and the cron from claiming the same row at the same moment, but claim_due_webhook_deliveries autocommits before any POST is issued, so from then on ownership is only status='in_flight' and a later cycle's recoverStuckInFlight sweep can re-arm a row still queued behind an earlier cycle's serial loop. Delivery is at-least-once, which is what the public docs already tell receivers ("the same delivery id may arrive more than once ... idempotency is on you"). The comments contradicted that. No behaviour change. The kick does not create this window: the cron claims 50 rows serially against the same 20 s stuck threshold, which is wider than what a batch of 5 can open. Refs #1201 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
e171bffa97
commit
49ff234954
+10
-2
@@ -3,8 +3,9 @@
|
||||
*
|
||||
* Subscribes to every CoreEventType the v1 API surface emits and converts
|
||||
* each emission into N rows in `webhook_deliveries`: one per active webhook
|
||||
* subscribed to (company_id, event_type). The dispatcher cron picks them
|
||||
* up at next-minute boundary and POSTs to the receiver.
|
||||
* subscribed to (company_id, event_type). A dispatch cycle is then scheduled
|
||||
* immediately (see dispatch-kick.ts); the per-minute cron stays as the retry
|
||||
* and sweep path.
|
||||
*
|
||||
* Wired from lib/init.ts via registerWebhookHandler() so every API route
|
||||
* that calls ensureInitialized() gets the subscription wired exactly once.
|
||||
@@ -26,6 +27,7 @@ import type { CoreEventType } from '@/lib/events/types'
|
||||
import { createServiceClientNoCookies } from '@/lib/auth/api-keys'
|
||||
import { createLogger } from '@/lib/logger'
|
||||
import { API_V1_VERSION } from '@/lib/api/v1/version'
|
||||
import { kickWebhookDispatch } from './dispatch-kick'
|
||||
|
||||
const log = createLogger('webhooks/handler')
|
||||
|
||||
@@ -184,5 +186,11 @@ async function fanOutToWebhooks(args: {
|
||||
eventType: args.eventType,
|
||||
webhookCount: rows.length,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Deliver now instead of waiting for the next cron tick (#1201). Scheduled,
|
||||
// never awaited: see lib/webhooks/dispatch-kick.ts for why the emitter must
|
||||
// not block on a receiver's HTTP endpoint.
|
||||
kickWebhookDispatch()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user