Skip to content

Commit 7206134

Browse files
committed
Add ability to configure a worker_queue and remove Fabric::WebhookWorker
- This works by overriding the `perform_async` method. I was surprised this works, but it does. - Additionally, Fabric::WebhookWorker has been removed. Using it is not ideal as it silences errors that Stripe would normally retry.
1 parent d4fec14 commit 7206134

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,6 @@ StripeEvent.configure do |events|
123123
end
124124
```
125125

126-
#### Asynchronous usage
127-
128-
In order to make this asynchronous, you can use a Fabric::WebhookWorker to accomplish the same thing. For example:
129-
130-
```ruby
131-
events.subscribe 'customer.created' do |event|
132-
Fabric::WebhookWorker.perform(event.to_hash, 'plan_deleted')
133-
end
134-
```
135-
136126
#### Events
137127

138128
When a webhook is received, Fabric optionally supports storing a Fabric::Event model (corresponding with Stripe's Event) which can be stored. This is a minimal version of the event which does not contain the full contents of the webhook. Instead, it only stores the event id, webhook type, customer id, and api version, with the first 3 fields defining a unique event. This uniqueness is then used to check idempotence of events coming in from Stripe, ensuring you don't run your webhook code twice for the same event, even if Stripe sends it twice. If `Fabric.config.store_events` is set to `true` (the default), Fabric will perform these checks. This is implemented in all webhooks in the project using the `check_idempotence` method.
@@ -159,6 +149,7 @@ You can set global configuration options with `Fabric.configure`:
159149

160150
```ruby
161151
Fabric.configure do |c|
152+
c.worker_queue = 'critical'
162153
c.store_events = false
163154
c.persist_models = :all
164155
c.logger = Rails.logger

lib/fabric.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@
9494
require 'fabric/webhooks/subscription_deleted'
9595
require 'fabric/webhooks/review_opened'
9696
require 'fabric/webhooks/review_closed'
97-
require 'fabric/app/workers/worker.rb'
98-
require 'fabric/app/workers/webhook_worker.rb'
99-
require 'fabric/app/models/fabric/base.rb'
97+
require 'fabric/app/workers/worker'
98+
require 'fabric/app/models/fabric/base'
10099

101100
module Fabric
102101
autoload :BalanceTransaction, 'fabric/app/models/fabric/balance_transaction'
@@ -143,6 +142,7 @@ class Config
143142
attr_accessor :store_events
144143
attr_accessor :store_event_data
145144
attr_accessor :logger
145+
attr_accessor :worker_queue
146146
attr_accessor :worker_callback
147147
attr_accessor :persist_models
148148
attr_accessor :currencies
@@ -151,6 +151,7 @@ def initialize
151151
@store_events = true
152152
@store_event_data = false
153153
@logger = ActiveSupport::Logger.new($stdout)
154+
@worker_queue = 'critical'
154155
@worker_callback = Proc.new {}
155156
@persist_models = :all
156157
@currencies = %w(usd)

lib/fabric/app/workers/webhook_worker.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/fabric/app/workers/worker.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Worker
33
include Fabric
44
include Sidekiq::Worker
55

6-
sidekiq_options queue: 'fabric', retry: false
6+
sidekiq_options retry: false
77

88
def perform(operation, *args)
99
@log_data = { class: self.class.name, operation: operation, args: args }
@@ -33,5 +33,20 @@ def call_callback(type, error = nil)
3333
Fabric.config.worker_callback.call(*args)
3434
end
3535

36+
class << self
37+
38+
def perform_async(...)
39+
set(queue: Fabric.config.worker_queue).perform_async(...)
40+
end
41+
42+
def perform_in(interval, ...)
43+
set(queue: Fabric.config.worker_queue).perform_in(interval, ...)
44+
end
45+
46+
def perform_at(timestamp, ...)
47+
set(queue: Fabric.config.worker_queue).perform_at(timestamp, ...)
48+
end
49+
50+
end
3651
end
3752
end

0 commit comments

Comments
 (0)