Skip to content

Commit

Permalink
Move constantize to Sidekiq::Util, #3474
Browse files Browse the repository at this point in the history
I don’t believe we need the Psych patch anymore because Sidekiq 5 guarantees that jobs are executed within Rails::Executor so classes should load as normal.  This was not the case back in early 4.2.x, 4.2.0, 4.2.1, etc.
  • Loading branch information
mperham committed May 15, 2017
1 parent 1399963 commit f9781e1
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 29 deletions.
17 changes: 0 additions & 17 deletions lib/sidekiq/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,3 @@ def symbolize_keys

end
end

begin
require 'active_support/core_ext/string/inflections'
rescue LoadError
class String
def constantize
names = self.split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end if !"".respond_to?(:constantize)
end
2 changes: 1 addition & 1 deletion lib/sidekiq/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def dispatch(job_hash, queue)
# the Reloader. It handles code loading, db connection management, etc.
# Effectively this block denotes a "unit of work" to Rails.
@reloader.call do
klass = job_hash['class'.freeze].constantize
klass = Sidekiq::Util.constantize(job_hash['class'.freeze])
worker = klass.new
worker.jid = job_hash['jid'.freeze]
@retrier.local(worker, job_hash, queue) do
Expand Down
9 changes: 0 additions & 9 deletions lib/sidekiq/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Rails < ::Rails::Engine
Sidekiq.configure_server do |_|
if ::Rails::VERSION::MAJOR >= 5
Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
Psych::Visitors::ToRuby.prepend(Sidekiq::Rails::PsychAutoload)
end
end
end
Expand All @@ -48,13 +47,5 @@ def inspect
"#<Sidekiq::Rails::Reloader @app=#{@app.class.name}>"
end
end

module PsychAutoload
def resolve_class(klass_name)
klass_name && klass_name.constantize
rescue NameError
super
end
end
end if defined?(::Rails)
end
4 changes: 2 additions & 2 deletions lib/sidekiq/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def raw_push(payloads)
true
elsif Sidekiq::Testing.inline?
payloads.each do |job|
klass = job['class'].constantize
klass = Sidekiq::Util.constantize(job['class'])
job['id'] ||= SecureRandom.hex(12)
job_hash = Sidekiq.load_json(Sidekiq.dump_json(job))
klass.process_job(job_hash)
Expand Down Expand Up @@ -309,7 +309,7 @@ def drain_all
worker_classes = jobs.map { |job| job["class"] }.uniq

worker_classes.each do |worker_class|
worker_class.constantize.drain
Sidekiq::Util.constantize(worker_class).drain
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/sidekiq/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,16 @@ def fire_event(event, reverse=false)
end
arr.clear
end

def self.constantize(str)
names = str.split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end
end

0 comments on commit f9781e1

Please sign in to comment.