Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,6 @@ StripeEvent.configure do |events|
end
```

#### Asynchronous usage

In order to make this asynchronous, you can use a Fabric::WebhookWorker to accomplish the same thing. For example:

```ruby
events.subscribe 'customer.created' do |event|
Fabric::WebhookWorker.perform(event.to_hash, 'plan_deleted')
end
```

#### Events

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.
Expand All @@ -159,6 +149,7 @@ You can set global configuration options with `Fabric.configure`:

```ruby
Fabric.configure do |c|
c.worker_queue = 'critical'
c.store_events = false
c.persist_models = :all
c.logger = Rails.logger
Expand Down
7 changes: 4 additions & 3 deletions lib/fabric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@
require 'fabric/webhooks/subscription_deleted'
require 'fabric/webhooks/review_opened'
require 'fabric/webhooks/review_closed'
require 'fabric/app/workers/worker.rb'
require 'fabric/app/workers/webhook_worker.rb'
require 'fabric/app/models/fabric/base.rb'
require 'fabric/app/workers/worker'
require 'fabric/app/models/fabric/base'

module Fabric
autoload :BalanceTransaction, 'fabric/app/models/fabric/balance_transaction'
Expand Down Expand Up @@ -143,6 +142,7 @@ class Config
attr_accessor :store_events
attr_accessor :store_event_data
attr_accessor :logger
attr_accessor :worker_queue
attr_accessor :worker_callback
attr_accessor :persist_models
attr_accessor :currencies
Expand All @@ -151,6 +151,7 @@ def initialize
@store_events = true
@store_event_data = false
@logger = ActiveSupport::Logger.new($stdout)
@worker_queue = 'critical'
@worker_callback = Proc.new {}
@persist_models = :all
@currencies = %w(usd)
Expand Down
13 changes: 0 additions & 13 deletions lib/fabric/app/workers/webhook_worker.rb

This file was deleted.

17 changes: 16 additions & 1 deletion lib/fabric/app/workers/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Worker
include Fabric
include Sidekiq::Worker

sidekiq_options queue: 'fabric', retry: false
sidekiq_options retry: false

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

class << self

def perform_async(...)
set(queue: Fabric.config.worker_queue).perform_async(...)
end

def perform_in(interval, ...)
set(queue: Fabric.config.worker_queue).perform_in(interval, ...)
end

def perform_at(timestamp, ...)
set(queue: Fabric.config.worker_queue).perform_at(timestamp, ...)
end

end
end
end