Class: Spree::WebhookEndpoint
- Inherits:
-
Object
- Object
- Spree::WebhookEndpoint
- Includes:
- SingleStoreResource
- Defined in:
- app/models/spree/webhook_endpoint.rb
Constant Summary collapse
- AUTO_DISABLE_THRESHOLD =
Number of consecutive failed deliveries before auto-disabling
15
Instance Method Summary collapse
-
#auto_disabled? ⇒ Boolean
Check if the endpoint was auto-disabled.
-
#check_auto_disable! ⇒ Object
Check if auto-disable threshold has been reached and disable if so.
-
#disable!(reason: 'Automatically disabled after repeated delivery failures', notify: true) ⇒ Object
Disable this endpoint due to repeated failures.
-
#enable! ⇒ Object
Re-enable a previously disabled endpoint.
-
#send_test! ⇒ Spree::WebhookDelivery
Send a test/ping webhook to verify the endpoint is reachable.
-
#subscribed_events ⇒ Array<String>
Returns all events this endpoint is subscribed to.
-
#subscribed_to?(event_name) ⇒ Boolean
Check if this endpoint is subscribed to a specific event.
Instance Method Details
#auto_disabled? ⇒ Boolean
Check if the endpoint was auto-disabled
99 100 101 |
# File 'app/models/spree/webhook_endpoint.rb', line 99 def auto_disabled? disabled_at.present? end |
#check_auto_disable! ⇒ Object
Check if auto-disable threshold has been reached and disable if so.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/spree/webhook_endpoint.rb', line 105 def check_auto_disable! return if auto_disabled? consecutive_failures = webhook_deliveries .where(success: false) .where.not(delivered_at: nil) .order(delivered_at: :desc) .limit(AUTO_DISABLE_THRESHOLD) return if consecutive_failures.count < AUTO_DISABLE_THRESHOLD # Verify they're all failures (no successes interspersed) last_success = webhook_deliveries.successful.order(delivered_at: :desc).pick(:delivered_at) oldest_failure = consecutive_failures.last&.delivered_at if last_success.nil? || (oldest_failure && oldest_failure > last_success) disable! end end |
#disable!(reason: 'Automatically disabled after repeated delivery failures', notify: true) ⇒ Object
Disable this endpoint due to repeated failures. Sends a notification email to the store staff.
86 87 88 89 |
# File 'app/models/spree/webhook_endpoint.rb', line 86 def disable!(reason: 'Automatically disabled after repeated delivery failures', notify: true) update!(active: false, disabled_reason: reason, disabled_at: Time.current) Spree::WebhookMailer.endpoint_disabled(self).deliver_later if notify end |
#enable! ⇒ Object
Re-enable a previously disabled endpoint.
92 93 94 |
# File 'app/models/spree/webhook_endpoint.rb', line 92 def enable! update!(active: true, disabled_reason: nil, disabled_at: nil) end |
#send_test! ⇒ Spree::WebhookDelivery
Send a test/ping webhook to verify the endpoint is reachable. Creates a delivery record and queues it for delivery.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/spree/webhook_endpoint.rb', line 65 def send_test! delivery = webhook_deliveries.create!( event_name: 'webhook.test', payload: { id: SecureRandom.uuid, name: 'webhook.test', created_at: Time.current.iso8601, data: { message: 'This is a test webhook from Spree.' }, metadata: { spree_version: Spree.version } } ) delivery.queue_for_delivery! delivery end |
#subscribed_events ⇒ Array<String>
Returns all events this endpoint is subscribed to
55 56 57 58 59 |
# File 'app/models/spree/webhook_endpoint.rb', line 55 def subscribed_events return ['*'] if subscriptions.blank? subscriptions end |
#subscribed_to?(event_name) ⇒ Boolean
Check if this endpoint is subscribed to a specific event
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/spree/webhook_endpoint.rb', line 39 def subscribed_to?(event_name) return true if subscriptions.blank? || subscriptions.include?('*') subscriptions.any? do |subscription| if subscription.include?('*') pattern = Regexp.escape(subscription).gsub('\*', '.*') event_name.match?(Regexp.new("^#{pattern}$")) else subscription == event_name end end end |