Skip to content

Commit e36eb73

Browse files
andy-southmichael-gratton
authored andcommitted
Fix broken check for existing workers running a job in Sidekiq
1 parent d481048 commit e36eb73

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

lib/identity_spoke.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ def self.base_campaign_url(campaign_id)
4949
Settings.spoke.base_campaign_url ? sprintf(Settings.spoke.base_campaign_url, campaign_id.to_s) : nil
5050
end
5151

52-
def self.worker_currenly_running?(method_name)
52+
def self.worker_currently_running?(method_name, sync_id)
5353
workers = Sidekiq::Workers.new
5454
workers.each do |_process_id, _thread_id, work|
55-
matched_process = work["payload"]["args"] = [SYSTEM_NAME, method_name]
56-
if matched_process
55+
args = work["payload"]["args"]
56+
worker_sync_id = (args.count > 0) ? args[0] : nil
57+
worker_sync = worker_sync_id ? Sync.find_by(id: worker_sync_id) : nil
58+
next unless worker_sync
59+
worker_system = worker_sync.external_system
60+
worker_method_name = JSON.parse(worker_sync.external_system_params)["pull_job"]
61+
already_running = (worker_system == SYSTEM_NAME &&
62+
worker_method_name == method_name &&
63+
worker_sync_id != sync_id)
64+
if already_running
5765
puts ">>> #{SYSTEM_NAME.titleize} #{method_name} skipping as worker already running ..."
5866
return true
5967
end
@@ -79,7 +87,10 @@ def self.pull(sync_id, external_system_params)
7987

8088
def self.fetch_new_messages(sync_id, force: false)
8189
## Do not run method if another worker is currently processing this method
82-
yield 0, {}, {}, true if self.worker_currenly_running?(__method__.to_s)
90+
if self.worker_currently_running?(__method__.to_s, sync_id)
91+
yield 0, {}, {}, true
92+
return
93+
end
8394

8495
started_at = DateTime.now
8596
last_created_at = Time.parse($redis.with { |r| r.get 'spoke:messages:last_created_at' } || '2019-01-01 00:00:00')
@@ -188,7 +199,10 @@ def self.handle_new_message(sync_id, message_id)
188199

189200
def self.fetch_new_opt_outs(sync_id, force: false)
190201
## Do not run method if another worker is currently processing this method
191-
yield 0, {}, {}, true if self.worker_currenly_running?(__method__.to_s)
202+
if self.worker_currently_running?(__method__.to_s, sync_id)
203+
yield 0, {}, {}, true
204+
return
205+
end
192206

193207
if Settings.spoke.subscription_id
194208
started_at = DateTime.now
@@ -245,7 +259,10 @@ def self.handle_new_opt_out(sync_id, opt_out_id)
245259

246260
def self.fetch_active_campaigns(sync_id, force: false)
247261
## Do not run method if another worker is currently processing this method
248-
yield 0, {}, {}, true if self.worker_currenly_running?(__method__.to_s)
262+
if self.worker_currently_running?(__method__.to_s, sync_id)
263+
yield 0, {}, {}, true
264+
return
265+
end
249266

250267
active_campaigns = IdentitySpoke::Campaign.active
251268

0 commit comments

Comments
 (0)