diff --git a/lib/sidekiq/core_ext.rb b/lib/sidekiq/core_ext.rb index ca9009df..80276d2d 100644 --- a/lib/sidekiq/core_ext.rb +++ b/lib/sidekiq/core_ext.rb @@ -51,4 +51,34 @@ rescue LoadError end end +begin + require 'active_support/core_ext/hash/keys' +rescue LoadError + class Hash + def stringify_keys(hash) + hash.keys.each do |key| + hash[key.to_s] = hash.delete(key) + end + hash + end + end if !{}.responds_to(:stringify_keys) +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 !"".responds_to(:constantize) +end + diff --git a/lib/sidekiq/processor.rb b/lib/sidekiq/processor.rb index 605626e5..4249fd30 100644 --- a/lib/sidekiq/processor.rb +++ b/lib/sidekiq/processor.rb @@ -34,7 +34,7 @@ module Sidekiq defer do begin msg = Sidekiq.load_json(msgstr) - klass = constantize(msg['class']) + klass = msg['class'].constantize worker = klass.new stats(worker, msg, queue) do diff --git a/lib/sidekiq/util.rb b/lib/sidekiq/util.rb index ffc5151f..819bb709 100644 --- a/lib/sidekiq/util.rb +++ b/lib/sidekiq/util.rb @@ -1,5 +1,6 @@ require 'socket' require 'sidekiq/exception_handler' +require 'sidekiq/core_ext' module Sidekiq ## @@ -10,17 +11,6 @@ module Sidekiq EXPIRY = 60 * 60 * 24 - def constantize(camel_cased_word) - names = camel_cased_word.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 - def watchdog(last_words) yield rescue Exception => ex diff --git a/lib/sidekiq/worker.rb b/lib/sidekiq/worker.rb index 080ab36c..be569a0c 100644 --- a/lib/sidekiq/worker.rb +++ b/lib/sidekiq/worker.rb @@ -31,6 +31,7 @@ module Sidekiq end module ClassMethods + def perform_async(*args) client_push('class' => self, 'args' => args) end @@ -52,7 +53,7 @@ module Sidekiq # :backtrace - whether to save any error backtrace in the retry payload to display in web UI, # can be true, false or an integer number of lines to save, default *false* def sidekiq_options(opts={}) - self.sidekiq_options_hash = get_sidekiq_options.merge(stringify_keys(opts || {})) + self.sidekiq_options_hash = get_sidekiq_options.merge((opts || {}).stringify_keys) end DEFAULT_OPTIONS = { 'retry' => true, 'queue' => 'default' } @@ -61,15 +62,8 @@ module Sidekiq self.sidekiq_options_hash ||= DEFAULT_OPTIONS end - def stringify_keys(hash) # :nodoc: - hash.keys.each do |key| - hash[key.to_s] = hash.delete(key) - end - hash - end - def client_push(item) # :nodoc: - Sidekiq::Client.push(stringify_keys(item)) + Sidekiq::Client.push(item.stringify_keys) end end