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

Add delay_until extension method

This commit is contained in:
Mike Perham 2012-11-08 09:05:11 -08:00
parent b7a1bb4d84
commit afb1d24809
5 changed files with 41 additions and 0 deletions

View file

@ -1,3 +1,14 @@
HEAD
-----------
- Add `delay_until` so you can delay jobs until a specific timestamp:
```ruby
Auction.delay_until(@auction.ends_at).close(@auction.id)
```
This is identical to the existing Sidekiq::Worker method, `perform_at`.
2.5.2 2.5.2
----------- -----------

View file

@ -30,6 +30,9 @@ module Sidekiq
def delay_for(interval, options={}) def delay_for(interval, options={})
Proxy.new(DelayedMailer, self, options.merge('at' => Time.now.to_f + interval.to_f)) Proxy.new(DelayedMailer, self, options.merge('at' => Time.now.to_f + interval.to_f))
end end
def delay_until(timestamp, options={})
Proxy.new(DelayedMailer, self, options.merge('at' => timestamp.to_f))
end
end end
end end

View file

@ -27,6 +27,9 @@ module Sidekiq
def delay_for(interval, options={}) def delay_for(interval, options={})
Proxy.new(DelayedModel, self, options.merge('at' => Time.now.to_f + interval.to_f)) Proxy.new(DelayedModel, self, options.merge('at' => Time.now.to_f + interval.to_f))
end end
def delay_until(timestamp, options={})
Proxy.new(DelayedModel, self, options.merge('at' => timestamp.to_f))
end
end end
end end

View file

@ -25,6 +25,9 @@ module Sidekiq
def delay_for(interval, options={}) def delay_for(interval, options={})
Proxy.new(DelayedClass, self, options.merge('at' => Time.now.to_f + interval.to_f)) Proxy.new(DelayedClass, self, options.merge('at' => Time.now.to_f + interval.to_f))
end end
def delay_until(timestamp, options={})
Proxy.new(DelayedClass, self, options.merge('at' => timestamp.to_f))
end
end end
end end

View file

@ -43,6 +43,12 @@ class TestExtensions < MiniTest::Unit::TestCase
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') } assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
end end
it 'allows until delayed scheduling of AR class methods' do
assert_equal 0, Sidekiq.redis {|c| c.zcard('schedule') }
MyModel.delay_until(1.day.from_now).long_class_method
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
end
class UserMailer < ActionMailer::Base class UserMailer < ActionMailer::Base
def greetings(a, b) def greetings(a, b)
raise "Should not be called!" raise "Should not be called!"
@ -63,13 +69,21 @@ class TestExtensions < MiniTest::Unit::TestCase
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') } assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
end end
it 'allows until delay scheduling of AM mails' do
assert_equal 0, Sidekiq.redis {|c| c.zcard('schedule') }
UserMailer.delay_until(5.days.from_now).greetings(1, 2)
assert_equal 1, Sidekiq.redis {|c| c.zcard('schedule') }
end
class SomeClass class SomeClass
def self.doit(arg) def self.doit(arg)
end end
end end
it 'allows delay of any ole class method' do it 'allows delay of any ole class method' do
assert_equal 0, queue_size
SomeClass.delay.doit(Date.today) SomeClass.delay.doit(Date.today)
assert_equal 1, queue_size
end end
module SomeModule module SomeModule
@ -78,7 +92,14 @@ class TestExtensions < MiniTest::Unit::TestCase
end end
it 'allows delay of any module class method' do it 'allows delay of any module class method' do
assert_equal 0, queue_size
SomeModule.delay.doit(Date.today) SomeModule.delay.doit(Date.today)
assert_equal 1, queue_size
end
def queue_size(name='default')
Sidekiq::Queue.new(name).size
end end
end end
end end