Skip to content

Commit 6fd5cf2

Browse files
committed
Add ability to configure a worker_queue
This works by overriding the `perform_async` method. I was surprised this works, but it does.
1 parent d4fec14 commit 6fd5cf2

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

lib/fabric.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@
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/base_worker'
98+
require 'fabric/app/workers/worker'
99+
require 'fabric/app/workers/webhook_worker'
100+
require 'fabric/app/models/fabric/base'
100101

101102
module Fabric
102103
autoload :BalanceTransaction, 'fabric/app/models/fabric/balance_transaction'
@@ -146,6 +147,7 @@ class Config
146147
attr_accessor :worker_callback
147148
attr_accessor :persist_models
148149
attr_accessor :currencies
150+
attr_accessor :worker_queue
149151

150152
def initialize
151153
@store_events = true
@@ -154,6 +156,7 @@ def initialize
154156
@worker_callback = Proc.new {}
155157
@persist_models = :all
156158
@currencies = %w(usd)
159+
@worker_queue = 'fabric'
157160
end
158161

159162
def persist?(document)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Fabric
2+
class BaseWorker
3+
include Fabric
4+
include Sidekiq::Worker
5+
6+
def self.perform_async(...)
7+
set(queue: Fabric.config.worker_queue).perform_async(...)
8+
end
9+
10+
def self.perform_in(interval, ...)
11+
set(queue: Fabric.config.worker_queue).perform_in(interval, ...)
12+
end
13+
14+
def self.perform_at(timestamp, ...)
15+
set(queue: Fabric.config.worker_queue).perform_at(timestamp, ...)
16+
end
17+
18+
end
19+
end
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
module Fabric
2-
class WebhookWorker
3-
include Sidekiq::Worker
4-
5-
sidekiq_options queue: 'fabric'
2+
class WebhookWorker < BaseWorker
63

74
def perform(event, webhook_class)
85
log_data = { event: event['id'], webhook_class: webhook_class }
96
Fabric.config.logger.info("started #{log_data.to_json}")
107
"Fabric::Webhooks::#{webhook_class.camelcase}".constantize.new.call(event)
118
end
9+
1210
end
1311
end

lib/fabric/app/workers/worker.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
module Fabric
2-
class Worker
3-
include Fabric
4-
include Sidekiq::Worker
2+
class Worker < BaseWorker
53

6-
sidekiq_options queue: 'fabric', retry: false
4+
sidekiq_options retry: false
75

86
def perform(operation, *args)
97
@log_data = { class: self.class.name, operation: operation, args: args }

0 commit comments

Comments
 (0)