Permalink
Browse files

allow passing options to #delay to control queue, retry etc.

  • Loading branch information...
1 parent 0559df1 commit 3bb31489a951f2ec36c399eb090f4dee93545cfc @elado committed Aug 18, 2012
View
2 .gitignore
@@ -4,3 +4,5 @@ Gemfile.lock
dump.rdb
.rbx
coverage/
+
+/test/db/test.sqlite3
View
9 lib/sidekiq/extensions/action_mailer.rb
@@ -21,11 +21,12 @@ def perform(*msg)
end
module ActionMailer
- def delay
- Proxy.new(DelayedMailer, self)
+ def delay(options={})
+ Proxy.new(DelayedMailer, self, options)
end
- def delay_for(interval)
- Proxy.new(DelayedMailer, self, Time.now.to_f + interval.to_f)
+ def delay_for(interval, options={})
+ options = options.reverse_merge(at: Time.now.to_f + interval.to_f)
+ delay(options)
end
end
View
9 lib/sidekiq/extensions/active_record.rb
@@ -28,11 +28,12 @@ def sidekiq_deserialize(string)
end
module InstanceMethods
- def delay
- Proxy.new(DelayedModel, self)
+ def delay(options={})
+ Proxy.new(DelayedModel, self, options)
end
- def delay_for(interval)
- Proxy.new(DelayedModel, self, Time.now.to_f + interval.to_f)
+ def delay_for(interval, options={})
+ options = options.reverse_merge(at: Time.now.to_f + interval.to_f)
+ delay(options)
end
def sidekiq_serialize
View
9 lib/sidekiq/extensions/class_methods.rb
@@ -19,11 +19,12 @@ def perform(*msg)
end
module Klass
- def delay
- Proxy.new(DelayedClass, self)
+ def delay(options={})
+ Proxy.new(DelayedClass, self, options)
end
- def delay_for(interval)
- Proxy.new(DelayedClass, self, Time.now.to_f + interval.to_f)
+ def delay_for(interval, options={})
+ options = options.reverse_merge(at: Time.now.to_f + interval.to_f)
+ delay(options)
end
def sidekiq_serialize
View
10 lib/sidekiq/extensions/generic_proxy.rb
@@ -1,10 +1,10 @@
module Sidekiq
module Extensions
class Proxy < BasicObject
- def initialize(performable, target, at=nil)
+ def initialize(performable, target, options={})
@performable = performable
@target = target
- @at = at
+ @options = options
end
def method_missing(name, *args)
@@ -15,11 +15,7 @@ def method_missing(name, *args)
# Ruby object.
serialized_args = ArgsSerializer.serialize_message(@target, name, *args)
- if @at
- @performable.perform_at(@at, *serialized_args)
- else
- @performable.perform_async(*serialized_args)
- end
+ @performable.perform_async_with_options(@options, *serialized_args)
end
end
View
9 lib/sidekiq/testing/inline.rb
@@ -36,6 +36,15 @@ def perform_async(*args)
)
true
end
+
+ def perform_async_with_options(options, *args)
+ new.perform(*
+ Sidekiq::Extensions::ArgsSerializer.deserialize(
+ Sidekiq::Extensions::ArgsSerializer.serialize(args)
+ )
+ )
+ true
+ end
end
end
end
View
9 lib/sidekiq/worker.rb
@@ -35,6 +35,15 @@ def perform_async(*args)
client_push('class' => self, 'args' => args)
end
+ def perform_async_with_options(options, *args)
+ if options[:at]
+ int = options[:at].to_f
+ options[:at] = (int < 1_000_000_000 ? Time.now.to_f + int : int)
+ end
+ options = stringify_keys(options)
+ client_push(options.merge('class' => self, 'args' => args))
+ end
+
def perform_in(interval, *args)
int = interval.to_f
ts = (int < 1_000_000_000 ? Time.now.to_f + int : int)
View
BIN test/db/test.sqlite3
Binary file not shown.
View
11 test/test_extensions.rb
@@ -55,7 +55,7 @@ def perform_last_job!(performer)
it 'allows delayed execution of ActiveRecord instance methods' do
assert_equal [], Sidekiq::Client.registered_queues
assert_equal 0, Sidekiq.redis {|c| c.llen('queue:default') }
- user = User.create
+ user = User.create!
user.delay.long_instance_method("with_argument")
assert_equal ['default'], Sidekiq::Client.registered_queues
assert_equal 1, Sidekiq.redis {|c| c.llen('queue:default') }
@@ -69,6 +69,15 @@ def perform_last_job!(performer)
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
end
+ it 'allows setting queue from options' do
+ assert_equal [], Sidekiq::Client.registered_queues
+ assert_equal 0, Sidekiq.redis {|c| c.llen('queue:custom_queue') }
+ user = User.create!
+ user.delay(queue: :custom_queue).long_instance_method("with_argument")
+ assert_equal ['custom_queue'], Sidekiq::Client.registered_queues
+ assert_equal 1, Sidekiq.redis {|c| c.llen('queue:custom_queue') }
+ end
+
ActionMailer::Base.perform_deliveries = false
class UserMailer < ActionMailer::Base

0 comments on commit 3bb3148

Please sign in to comment.