Skip to content

Commit d139af9

Browse files
fix: support indifferent access (#5)
Symbols vs. strings for `::Hash`. Co-authored-by: sam-albon-li <[email protected]>
1 parent 80a98de commit d139af9

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/interactor/sidekiq.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

3-
require 'interactor'
4-
require 'sidekiq'
5-
require 'active_support/core_ext/hash'
3+
require "interactor"
4+
require "sidekiq"
5+
require "active_support/core_ext/hash"
66

77
module Interactor
88
# Internal: Install Interactor's behavior in the given class.
@@ -27,18 +27,26 @@ class Worker
2727
include ::Sidekiq::Worker
2828

2929
sidekiq_retries_exhausted do |job, e|
30-
return if job['args'].blank? || job['args'].first['interactor_class'].blank?
30+
interactor_class = job.with_indifferent_access.dig(:args, 0, :interactor_class)&.constantize
3131

32-
interactor_class = job['args'].first['interactor_class'].constantize
33-
if interactor_class.respond_to?(:handle_sidekiq_retries_exhausted)
32+
if interactor_class.nil?
33+
return
34+
elsif interactor_class.respond_to?(:handle_sidekiq_retries_exhausted)
3435
interactor_class.handle_sidekiq_retries_exhausted(job, e)
3536
else
3637
raise e
3738
end
3839
end
3940

4041
def perform(interactor_context)
41-
interactor_class(interactor_context).sync_call(interactor_context.reject { |c| ['interactor_class'].include? c.to_s })
42+
# could be an interactor context object
43+
interactor_context = if interactor_context.respond_to?(:symbolize_keys)
44+
interactor_context.symbolize_keys
45+
else
46+
interactor_context
47+
end
48+
49+
interactor_class(interactor_context).sync_call(interactor_context.reject { ["interactor_class"].include?(_1.to_s) })
4250
rescue Exception => e
4351
if interactor_class(interactor_context).respond_to?(:handle_sidekiq_exception)
4452
interactor_class(interactor_context).handle_sidekiq_exception(e)
@@ -50,7 +58,7 @@ def perform(interactor_context)
5058
private
5159

5260
def interactor_class(interactor_context)
53-
Module.const_get interactor_context[:interactor_class]
61+
@interactor_context ||= Module.const_get interactor_context[:interactor_class]
5462
end
5563
end
5664

@@ -59,6 +67,13 @@ def sync_call(interactor_context = {})
5967
end
6068

6169
def async_call(interactor_context = {})
70+
# could be an interactor context object
71+
interactor_context = if interactor_context.respond_to?(:symbolize_keys)
72+
interactor_context.symbolize_keys
73+
else
74+
interactor_context
75+
end
76+
6277
options = handle_sidekiq_options(interactor_context)
6378
schedule_options = delay_sidekiq_schedule_options(interactor_context)
6479

0 commit comments

Comments
 (0)