Skip to content

Commit

Permalink
Only queue webhooks for events we're listening to
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Aug 25, 2021
1 parent f68b070 commit d89f306
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/pay/webhooks/braintree_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def create
private

def queue_event(event)
return unless Pay::Webhooks.delegator.listening?("braintree.#{event.kind}")

record = Pay::Webhook.create(
processor: :braintree,
event_type: event.kind,
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/pay/webhooks/paddle_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def create
private

def queue_event(event)
return unless Pay::Webhooks.delegator.listening?("paddle.#{params[:alert_name]}")

record = Pay::Webhook.create(processor: :paddle, event_type: params[:alert_name], event: event)
Pay::Webhooks::ProcessJob.perform_later(record)
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/pay/webhooks/stripe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def create
private

def queue_event(event)
return unless Pay::Webhooks.delegator.listening?("stripe.#{event.type}")

record = Pay::Webhook.create(processor: :stripe, event_type: event.type, event: event)
Pay::Webhooks::ProcessJob.perform_later(record)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pay/webhooks/delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def instrument(event:, type:)
backend.instrument name_with_namespace(type), event
end

def listening?(type)
backend.notifier.listening? name_with_namespace(type)
end

# Strips down to event data only
class NotificationAdapter
def initialize(subscriber)
Expand Down
6 changes: 6 additions & 0 deletions test/pay/webhooks/delegator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def call(event)
assert_equal 2, results.length
end

test "listening?" do
refute delegator.listening?("example.test_event")
delegator.subscribe "example.test_event", ->(event) {}
assert delegator.listening?("example.test_event")
end

private

attr_reader :delegator
Expand Down

0 comments on commit d89f306

Please sign in to comment.