Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: failed webhook deliveries are retried with backoff (#14723)

Damian Legawiec897450460ce5
diff --git a/docs/developer/core-concepts/webhooks.mdx b/docs/developer/core-concepts/webhooks.mdx
index e8ed993a99..d615d6356c 100644
--- a/docs/developer/core-concepts/webhooks.mdx
+++ b/docs/developer/core-concepts/webhooks.mdx
@@ -12,7 +12,7 @@ Webhooks are built on top of Spree's [event system](/developer/core-concepts/eve
- **Multi-store support** - Each store has its own webhook endpoints
- **Event filtering** - Subscribe to specific events or patterns with wildcards
- **Secure delivery** - HMAC-SHA256 signatures for payload verification
-- **Failure handling** - Failed deliveries are recorded and can be re-sent, and an endpoint that keeps failing is disabled
+- **Failure handling** - Failed deliveries are retried automatically and can be re-sent by hand, and an endpoint that keeps failing is disabled
- **Full audit trail** - Track every delivery attempt with response codes and timing
## How Webhooks Work
@@ -275,7 +275,9 @@ end
### Failed Deliveries
-Spree sends each delivery once. Any response other than 2xx — including a timeout or a connection error — marks the delivery as failed, and Spree does not send it again on its own. To send a failed delivery again, re-send it from the delivery log (see below).
+Any response other than 2xx — including a timeout or a connection error — marks the delivery as failed. Spree then sends it again, waiting longer before each try, up to 5 attempts in total over about six minutes. The delivery log shows the outcome of the latest attempt. Retries stop as soon as the endpoint is turned off.
+
+Every attempt carries the same event `id`, so use it to ignore an event your receiver has already processed. To send a delivery again after the retries run out, re-send it from the delivery log (see below).
If an endpoint fails 15 deliveries in a row, Spree disables it and emails the store staff. Fix the endpoint, then turn it back on by marking it active again.
diff --git a/docs/developer/tutorial/events.mdx b/docs/developer/tutorial/events.mdx
index 51a0047576..36cc9df347 100644
--- a/docs/developer/tutorial/events.mdx
+++ b/docs/developer/tutorial/events.mdx
@@ -63,7 +63,7 @@ When the consumer is a system you do not deploy your own code into, use a webhoo
Each delivery is an HTTP POST carrying the event name and the same serialized payload. The endpoint's **signing secret is shown once when you create it**: store it in the receiving system and verify every request against it, so the receiver can prove the call came from your store.
-Deliveries are retried with exponential backoff, and the dashboard shows the attempt history per endpoint, which is where to look when a third party says they never received something.
+Failed deliveries are retried with increasing delays, and the dashboard shows the delivery history per endpoint, which is where to look when a third party says they never received something.
<Tip>
Webhooks need no code in your app at all. Reach for a subscriber only when the reaction needs application context or credentials you would rather not hand to a third party. See [events](/developer/core-concepts/events) and [webhooks](/developer/core-concepts/webhooks).
diff --git a/spree/api/app/jobs/spree/webhook_delivery_job.rb b/spree/api/app/jobs/spree/webhook_delivery_job.rb
index 5034273caf..7f2dd1eae5 100644
--- a/spree/api/app/jobs/spree/webhook_delivery_job.rb
+++ b/spree/api/app/jobs/spree/webhook_delivery_job.rb
@@ -2,11 +2,19 @@
module Spree
class WebhookDeliveryJob < Spree::BaseJob
+ # Raised when the endpoint answered with anything other than 2xx, timed
+ # out or could not be reached, so the delivery is sent again with backoff.
+ class DeliveryFailed < StandardError; end
+
queue_as Spree.queues.webhooks
- # Webhook delivery hits external endpoints; broad retry covers network timeouts,
- # 5xx, DNS failures, etc.
+ # Delivery is retry-safe: each attempt overwrites the outcome on the same
+ # delivery record, and receivers deduplicate on the event id.
retry_on StandardError, wait: :polynomially_longer, attempts: 5
+ # The outcome of the last attempt is already on the delivery record, so an
+ # exhausted retry is not re-raised into the queue backend.
+ retry_on DeliveryFailed, wait: :polynomially_longer, attempts: 5 do |_job, _error|
+ end
# Must come after `retry_on StandardError` so DeserializationError lands in discard
# (ActiveJob handler lookup is reverse-declaration-order).
discard_on ActiveJob::DeserializationError
@@ -21,9 +29,15 @@ module Spree
delivery = Spree::WebhookDelivery.find_by(id: delivery_id)
return if delivery.nil?
+ endpoint = delivery.webhook_endpoint
+ return if endpoint.nil?
+ # A retry stops once the endpoint was switched off, by hand or by auto-disable.
+ return if executions > 1 && !endpoint.active?
+
delivery.payload_secrets = payload_secrets
- secret_key = delivery.webhook_endpoint.secret_key
- Spree::Webhooks::DeliverWebhook.call(delivery: delivery, secret_key: secret_key)
+ Spree::Webhooks::DeliverWebhook.call(delivery: delivery, secret_key: endpoint.secret_key)
+
+ raise DeliveryFailed, "Webhook delivery #{delivery.prefixed_id} failed" if delivery.failed?
end
end
end
diff --git a/spree/api/spec/jobs/spree/webhook_delivery_job_spec.rb b/spree/api/spec/jobs/spree/webhook_delivery_job_spec.rb
index 61bd5a819d..dc520deb6d 100644
--- a/spree/api/spec/jobs/spree/webhook_delivery_job_spec.rb
+++ b/spree/api/spec/jobs/spree/webhook_delivery_job_spec.rb
@@ -42,6 +42,42 @@ module Spree
end
end
+ describe 'when the endpoint fails' do
+ before { stub_request(:post, delivery.url).to_return(status: 500, body: 'error') }
+
+ it 'schedules another attempt with the same arguments' do
+ expect { described_class.perform_now(delivery.id, payload_secrets: { 'token' => 'x' }) }.
+ to have_enqueued_job(described_class).with(delivery.id, payload_secrets: { 'token' => 'x' })
+ expect(delivery.reload).to be_failed
+ end
+
+ it 'stops after five attempts without raising' do
+ expect { perform_enqueued_jobs { described_class.perform_later(delivery.id) } }.not_to raise_error
+
+ expect(WebMock).to have_requested(:post, delivery.url).times(5)
+ expect(enqueued_jobs).to be_empty
+ end
+
+ it 'sends again once the endpoint recovers' do
+ stub_request(:post, delivery.url).to_return({ status: 500 }, { status: 200, body: '{}' })
+
+ perform_enqueued_jobs { described_class.perform_later(delivery.id) }
+
+ expect(WebMock).to have_requested(:post, delivery.url).twice
+ expect(delivery.reload).to be_successful
+ end
+
+ it 'does not retry once the endpoint was switched off' do
+ webhook_endpoint.update!(active: false)
+ job = described_class.new(delivery.id)
+ job.executions = 1
+
+ job.perform_now
+
+ expect(WebMock).not_to have_requested(:post, delivery.url)
+ end
+ end
+
describe 'queue' do
it 'uses the webhooks queue' do
expect(described_class.new.queue_name).to eq(Spree.queues.webhooks.to_s)