Repository navigation
fix(webhooks): retry only the endpoints that failed, under a stable delivery id - #14025
Conversation
…elivery id A webhooks message fans one event out to every matching webhook and throws if any of them fails. On a broker that redelivers (NATS), the redelivery ran the whole loop again, so every endpoint that had already accepted the event received it a second time, up to maxDeliver times, and its "sent" usage was counted again. The publisher now names each event (eventId in the payload, which every broker carries through a retry unchanged; the Redis broker requeues under a fresh pid). When a delivery partially fails, the worker records the webhooks that accepted it in the cache for 7 days, and a redelivery skips them and retries only the rest. The record is written only on a partial failure and read only on a redelivery, so the common path is unchanged. An unreadable cache falls back to re-sending. Each request carries X-Appwrite-Webhook-Delivery-Id, derived from the event and the webhook: the same on every attempt of one event to one webhook, different for anything else, so a receiver can drop a repeat that still gets through (a worker killed mid-loop). Messages published before this change fall back to the pid, which is stable on NATS. The enabled check moves into the loop, so a webhook paused between attempts is skipped rather than counted as delivered.
🟢 Tier S · Ready to merge
Adds stable webhook delivery IDs and cached success tracking so broker redeliveries retry only failed endpoints, alongside library and E2E coverage. Updates queue-retry limit handling and makes an omitted NATS limit use the initial pending-message count rather than a fixed 500-job cap. CI also isolates queue-retry tests and adds security-rule reporting and native-crash checks. Latest changes: The newest commits replace NATS's default 500-job retry cap with the retry consumer's initial pending count and add an E2E regression covering 501 dead-lettered messages.
📂 Walkthrough · 9
✅ Fixed since the last review · 1
Reviewed the commits since |
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Move the redelivery bookkeeping into Appwrite\Event\Redelivery, unit-test that library, and replace the worker unit tests with an E2E test that forces the retry through queue-retry.
…the record without its key
…uous attempts check queue-retry re-drives every failed job in the queue, so the test moves to a queueRetry group that CI runs in the grouped job, where no other test's jobs are in flight, and the service job excludes it. The attempts reset after the retry was set by the webhook update, not the worker; the webhook URL only accepts allowed hosts, so the endpoint cannot recover without that update. Assert the real failure count before the update instead.
|
Findings from the latest review summaries:
|
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
|
Review complete. 🟡 1 medium 📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — queue-retry reads missing --limit as 0: silent no-op · src/Appwrite/Platform/Tasks/QueueRetry.php
🧹 Nitpicks (2) — 🟢 2 low
The change makes webhook fan-out idempotent across broker retries:
Reviewed commit: 85257df |
Resolves the conflict in the webhooks worker with main's SSRF fix (#3eea7049f1): the worker now takes the injected `clientForWebhooks`, whose destination policy replaces the PublicHostname check that used to run in execute(), so that check is gone here too. On top of it this branch keeps its own changes: `cache` is injected after `clientForWebhooks`, disabled and unsubscribed webhooks are skipped before the redelivery record is consulted, a webhook the record marks as delivered is skipped, and execute() sends the stable X-Appwrite-Webhook-Delivery-Id through the policy-checked client.
Security rulesNo new WARNING or ERROR findings from security rules. 32 existing findings tracked in
|
The task cast a missing --limit to 0, which every broker reads as 'retry nothing', so the plain 'queue-retry --name=<queue>' exited 0 and moved nothing. An omitted limit is now null (no cap) and anything but a positive whole number is refused. The webhooks retry E2E now runs the task without --limit, the way an operator does, and the orphaned listWebhooks docblock is back above its method.
|
@tenki-reviewer thanks: 🟡
🟢 Orphan docblock: fixed in 890e502. The 🟢 The redelivery record is written only after the loop and only on failure: deliberate, not changed.
|
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR introduces at-least-once redelivery support for webhooks: the webhooks worker now skips targets already handled in a prior attempt, records delivered targets in a TTL-ed cache, and sends a delivery id header so receivers can dedup.
Reviewed commit: 890e502 |
There was a problem hiding this comment.
Adds cache-backed webhook redelivery deduplication with eventId-based delivery ids, QueueRetry limit validation, and grouped e2e coverage.
Key findings
- 🟡 Null
--limitis capped at 500 on NATS — QueueRetry.php:42
…imit Broker\Nats::retry() read a null limit as 500, while Broker\Redis reads it as no cap, so 'queue-retry' without --limit left a large NATS dead stream partly re-driven. Null now means every message on the dead stream when the sweep starts: the bound is read from the retry consumer up front, so a message this sweep re-drives that dies again is not picked up a second time by the same run.
|
@tenki-reviewer on the nitpick untracked messages get unstable delivery ids: that's expected and harmless. A message without |
|
Review complete. 🟡 1 medium 📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Null-limit retry bound misses ack-pending dead messages · Nats.php:1057–1067 · // packages/queue/src/Broker/Nats.php
1057 $remaining = $limit ?? $consumer->info(true)->numPending;
1058 while ($remaining > 0) {
1059 $jsMessage = $this->fetch($consumer, 1, 1.0, false)[0] ?? null;
1060 if (!$jsMessage instanceof JetStreamMessage) {
1061 break;
1062 }
1063 // Re-drive onto the work queue, then remove it from the dead stream.
1064 $this->js()->publish($this->workSubject($queue), $jsMessage->getData());
1065 $jsMessage->ackSync();
1066 $remaining--;
1067 }The null-limit sweep bound If stranded ack-pending messages are the only dead letters, the next sweep computes a zero bound and never fetches, so 🧹 Nitpicks (1) — 🟢 1 low
The change closes the gap where webhook messages parked on a dead stream or failed list were never re-driven. It introduces a retry sweep on the NATS broker, a
Reviewed commit: dd94aa4 |
Summary
A webhooks message fans one event out to every matching webhook and throws if any of them fails. On a broker that redelivers (NATS), the redelivery re-ran the whole loop: every endpoint that had already accepted the event got it again, up to
maxDelivertimes, and its "sent" usage was counted again. Now a redelivery retries only the endpoints that failed. Every request also carriesX-Appwrite-Webhook-Delivery-Id, which stays the same across attempts, so receivers can drop repeats.Part of appwrite-labs/cloud#5815, tracked in appwrite-labs/cloud#6215.
Why this shape
Event\Webhookadds aneventIdto the queue message. It is not part of the body sent to endpoints, which is stillpayload['payload']. The Redis broker requeues a retry under a fresh pid (Broker/Redis.phpretryPayload()), while the payload survives every broker, requeue and migration unchanged.md5(eventId:webhookId): the same on every attempt of one event to one webhook, and different for anything else. This follows GitHub'sX-GitHub-Delivery.getAttempts() > 0) skips them. The cache is written only on a partial failure and read only on a redelivery, so the normal path never touches it.attemptsand the pause threshold. Endpoints that already accepted are no longer re-sent to or re-counted.enabledcheck moved into the loop, so a webhook paused between attempts is skipped rather than recorded as delivered.eventIdand fall back to the pid, which is stable on NATS.Evidence
Tests
WebhooksBase::testRedeliveryRetriesOnlyTheWebhookThatFailed.queue-retry.attemptsreset to 0, and the two delivery ids are non-empty and different.mainthe healthy webhook is sent the event again ("size 2, expected 1").testTablesDBRowsalso asserts 4 distinct delivery ids across 2 events × 2 webhooks. The whole Webhooks suite passes locally (135 tests).Appwrite\Event\Redelivery, shared with the functions worker (fix(functions): never redeliver a message once the function has run #14023), with 10 tests intests/unit/Event/RedeliveryTest.php. The file is byte-identical in both PRs.Not covered
X-Appwrite-Webhook-Delivery-Idadded._APP_WEBHOOK_MAX_FAILED_ATTEMPTS) in fewer events than on Redis, because each event is retried. That was already true before this PR and is a separate decision.