1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Use core_ext for constantize and stringify_keys

This commit is contained in:
Mike Perham 2012-10-27 12:48:34 -07:00
parent ad03228cb6
commit f04e1ecbec
4 changed files with 35 additions and 21 deletions

View file

@ -51,4 +51,34 @@ rescue LoadError
end end
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

View file

@ -34,7 +34,7 @@ module Sidekiq
defer do defer do
begin begin
msg = Sidekiq.load_json(msgstr) msg = Sidekiq.load_json(msgstr)
klass = constantize(msg['class']) klass = msg['class'].constantize
worker = klass.new worker = klass.new
stats(worker, msg, queue) do stats(worker, msg, queue) do

View file

@ -1,5 +1,6 @@
require 'socket' require 'socket'
require 'sidekiq/exception_handler' require 'sidekiq/exception_handler'
require 'sidekiq/core_ext'
module Sidekiq module Sidekiq
## ##
@ -10,17 +11,6 @@ module Sidekiq
EXPIRY = 60 * 60 * 24 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) def watchdog(last_words)
yield yield
rescue Exception => ex rescue Exception => ex

View file

@ -31,6 +31,7 @@ module Sidekiq
end end
module ClassMethods module ClassMethods
def perform_async(*args) def perform_async(*args)
client_push('class' => self, 'args' => args) client_push('class' => self, 'args' => args)
end end
@ -52,7 +53,7 @@ module Sidekiq
# :backtrace - whether to save any error backtrace in the retry payload to display in web UI, # :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* # can be true, false or an integer number of lines to save, default *false*
def sidekiq_options(opts={}) 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 end
DEFAULT_OPTIONS = { 'retry' => true, 'queue' => 'default' } DEFAULT_OPTIONS = { 'retry' => true, 'queue' => 'default' }
@ -61,15 +62,8 @@ module Sidekiq
self.sidekiq_options_hash ||= DEFAULT_OPTIONS self.sidekiq_options_hash ||= DEFAULT_OPTIONS
end 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: def client_push(item) # :nodoc:
Sidekiq::Client.push(stringify_keys(item)) Sidekiq::Client.push(item.stringify_keys)
end end
end end