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

Move class_attribute impl into Sidekiq::Worker

This commit is contained in:
Mike Perham 2017-05-15 11:17:22 -07:00
parent ddc40b2070
commit 1b3f70a1e5
4 changed files with 63 additions and 83 deletions

View file

@ -1,75 +0,0 @@
# frozen_string_literal: true
begin
require 'active_support/core_ext/class/attribute'
rescue LoadError
# A dumbed down version of ActiveSupport 5.1.0's
# Class#class_attribute helper.
class Module
# Removes the named method, if it exists.
def remove_possible_method(method)
if method_defined?(method) || private_method_defined?(method)
undef_method(method)
end
end
# Removes the named singleton method, if it exists.
def remove_possible_singleton_method(method)
singleton_class.instance_eval do
remove_possible_method(method)
end
end
end
class Class
def class_attribute(*attrs)
instance_reader = true
instance_writer = true
attrs.each do |name|
remove_possible_singleton_method(name)
define_singleton_method(name) { nil }
ivar = "@#{name}"
remove_possible_singleton_method("#{name}=")
define_singleton_method("#{name}=") do |val|
singleton_class.class_eval do
remove_possible_method(name)
define_method(name) { val }
end
if singleton_class?
class_eval do
remove_possible_method(name)
define_method(name) do
if instance_variable_defined? ivar
instance_variable_get ivar
else
singleton_class.send name
end
end
end
end
val
end
if instance_reader
remove_possible_method name
define_method(name) do
if instance_variable_defined?(ivar)
instance_variable_get ivar
else
self.class.public_send name
end
end
end
if instance_writer
remove_possible_method "#{name}="
attr_writer name
end
end
end
end
end

View file

@ -203,7 +203,7 @@ module Sidekiq
end
def delay_for(worker, count, exception)
worker && worker.sidekiq_retry_in_block? && retry_in(worker, count, exception) || seconds_to_delay(count)
worker && worker.sidekiq_retry_in_block && retry_in(worker, count, exception) || seconds_to_delay(count)
end
# delayed_job uses the same basic formula

View file

@ -4,7 +4,6 @@ require 'sidekiq/core_ext'
module Sidekiq
##
# Include this module in your worker class and you can easily create
# asynchronous jobs:
@ -149,6 +148,62 @@ module Sidekiq
Sidekiq::Client.new(pool).push(item)
end
def class_attribute(*attrs)
instance_reader = true
instance_writer = true
attrs.each do |name|
singleton_class.instance_eval do
undef_method(name) if method_defined?(name) || private_method_defined?(name)
end
define_singleton_method(name) { nil }
ivar = "@#{name}"
singleton_class.instance_eval do
m = "#{name}="
undef_method(m) if method_defined?(m) || private_method_defined?(m)
end
define_singleton_method("#{name}=") do |val|
singleton_class.class_eval do
undef_method(name) if method_defined?(name) || private_method_defined?(name)
define_method(name) { val }
end
if singleton_class?
class_eval do
undef_method(name) if method_defined?(name) || private_method_defined?(name)
define_method(name) do
if instance_variable_defined? ivar
instance_variable_get ivar
else
singleton_class.send name
end
end
end
end
val
end
if instance_reader
undef_method(name) if method_defined?(name) || private_method_defined?(name)
define_method(name) do
if instance_variable_defined?(ivar)
instance_variable_get ivar
else
self.class.public_send name
end
end
end
if instance_writer
m = "#{name}="
undef_method(m) if method_defined?(m) || private_method_defined?(m)
attr_writer name
end
end
end
end
end
end

View file

@ -64,7 +64,7 @@ class TestRetryExhausted < Sidekiq::Test
# successful
end
refute NewWorker.exhausted_called?
refute NewWorker.exhausted_called
end
it 'does not run exhausted block when job successful on last retry' do
@ -72,7 +72,7 @@ class TestRetryExhausted < Sidekiq::Test
# successful
end
refute NewWorker.exhausted_called?
refute NewWorker.exhausted_called
end
it 'does not run exhausted block when retries not exhausted yet' do
@ -82,7 +82,7 @@ class TestRetryExhausted < Sidekiq::Test
end
end
refute NewWorker.exhausted_called?
refute NewWorker.exhausted_called
end
it 'runs exhausted block when retries exhausted' do
@ -92,7 +92,7 @@ class TestRetryExhausted < Sidekiq::Test
end
end
assert NewWorker.exhausted_called?
assert NewWorker.exhausted_called
end
@ -104,7 +104,7 @@ class TestRetryExhausted < Sidekiq::Test
end
raised_error = raised_error.cause
assert new_worker.exhausted_called?
assert new_worker.exhausted_called
assert_equal raised_error.message, new_worker.exhausted_job['error_message']
assert_equal raised_error, new_worker.exhausted_exception
end
@ -117,7 +117,7 @@ class TestRetryExhausted < Sidekiq::Test
end
raised_error = raised_error.cause
assert old_worker.exhausted_called?
assert old_worker.exhausted_called
assert_equal raised_error.message, old_worker.exhausted_job['error_message']
assert_nil new_worker.exhausted_exception
end