mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Enable proper inheritance of sidekiq options in worker subclasses [#221]
This commit is contained in:
parent
82da2cd62b
commit
0addaba4a6
5 changed files with 85 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
|||
HEAD
|
||||
-----------
|
||||
|
||||
- Worker subclasses now properly inherit sidekiq\_options set in
|
||||
their superclass [#221]
|
||||
- Add random jitter to scheduler to spread polls across POLL\_INTERVAL
|
||||
window. [#247]
|
||||
|
||||
|
|
54
lib/sidekiq/core_ext.rb
Normal file
54
lib/sidekiq/core_ext.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
begin
|
||||
require 'active_support/core_ext/class/attribute'
|
||||
rescue LoadError
|
||||
|
||||
# A dumbed down version of ActiveSupport's
|
||||
# Class#class_attribute helper.
|
||||
class Class
|
||||
def class_attribute(*attrs)
|
||||
instance_reader = true
|
||||
instance_writer = true
|
||||
|
||||
attrs.each do |name|
|
||||
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def self.#{name}() nil end
|
||||
def self.#{name}?() !!#{name} end
|
||||
|
||||
def self.#{name}=(val)
|
||||
singleton_class.class_eval do
|
||||
define_method(:#{name}) { val }
|
||||
end
|
||||
|
||||
if singleton_class?
|
||||
class_eval do
|
||||
def #{name}
|
||||
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
|
||||
end
|
||||
end
|
||||
end
|
||||
val
|
||||
end
|
||||
|
||||
if instance_reader
|
||||
def #{name}
|
||||
defined?(@#{name}) ? @#{name} : self.class.#{name}
|
||||
end
|
||||
|
||||
def #{name}?
|
||||
!!#{name}
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
attr_writer name if instance_writer
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def singleton_class?
|
||||
ancestors.first != self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
module Sidekiq
|
||||
VERSION = "2.0.2"
|
||||
VERSION = "2.0.3"
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'sidekiq/client'
|
||||
require 'sidekiq/core_ext'
|
||||
|
||||
module Sidekiq
|
||||
|
||||
|
@ -22,6 +23,7 @@ module Sidekiq
|
|||
module Worker
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
base.class_attribute :sidekiq_options_hash
|
||||
end
|
||||
|
||||
def logger
|
||||
|
@ -51,11 +53,13 @@ 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={})
|
||||
@sidekiq_options = get_sidekiq_options.merge(stringify_keys(opts || {}))
|
||||
self.sidekiq_options_hash = get_sidekiq_options.merge(stringify_keys(opts || {}))
|
||||
end
|
||||
|
||||
DEFAULT_OPTIONS = { 'unique' => true, 'retry' => true, 'queue' => 'default' }
|
||||
|
||||
def get_sidekiq_options # :nodoc:
|
||||
defined?(@sidekiq_options) ? @sidekiq_options : { 'unique' => true, 'retry' => true, 'queue' => 'default' }
|
||||
self.sidekiq_options_hash ||= DEFAULT_OPTIONS
|
||||
end
|
||||
|
||||
def stringify_keys(hash) # :nodoc:
|
||||
|
|
|
@ -73,6 +73,10 @@ class TestClient < MiniTest::Unit::TestCase
|
|||
include Sidekiq::Worker
|
||||
end
|
||||
|
||||
it 'has default options' do
|
||||
assert_equal Sidekiq::Worker::ClassMethods::DEFAULT_OPTIONS, MyWorker.get_sidekiq_options
|
||||
end
|
||||
|
||||
it 'handles perform_async' do
|
||||
@redis.expect :rpush, 1, ['queue:default', String]
|
||||
pushed = MyWorker.perform_async(1, 2)
|
||||
|
@ -116,4 +120,22 @@ class TestClient < MiniTest::Unit::TestCase
|
|||
assert_equal ['bob'], Sidekiq::Client.registered_workers
|
||||
end
|
||||
end
|
||||
|
||||
class BaseWorker
|
||||
include Sidekiq::Worker
|
||||
sidekiq_options 'retry' => 'base'
|
||||
end
|
||||
class AWorker < BaseWorker
|
||||
end
|
||||
class BWorker < BaseWorker
|
||||
sidekiq_options 'retry' => 'b'
|
||||
end
|
||||
|
||||
describe 'inheritance' do
|
||||
it 'should inherit sidekiq options' do
|
||||
assert_equal 'base', AWorker.get_sidekiq_options['retry']
|
||||
assert_equal 'b', BWorker.get_sidekiq_options['retry']
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue