mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Fix the error in worker#perform_in about interval type (#3170)
Situation: We are using Sidekiq Pro with ActiveSupport. When I passed a object which is ActiveSupport::TimeWithZone to perform_at, `TypeError: can't convert ActiveSupport::TimeWithZone into an exact number` has occurred. Problem: Time can't plus a ActiveSupport::TimeWithZone object. Solution: We can transform any Time object to float, and use it to compare and calculate.
This commit is contained in:
parent
9abf52f35a
commit
6b14010199
2 changed files with 11 additions and 3 deletions
|
@ -64,13 +64,13 @@ module Sidekiq
|
|||
# numeric (like an activesupport time interval).
|
||||
def perform_in(interval, *args)
|
||||
int = interval.to_f
|
||||
now = Time.now
|
||||
ts = (int < 1_000_000_000 ? (now + interval).to_f : int)
|
||||
now = Time.now.to_f
|
||||
ts = (int < 1_000_000_000 ? now + int : int)
|
||||
|
||||
item = { 'class' => self, 'args' => args, 'at' => ts }
|
||||
|
||||
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
||||
item.delete('at'.freeze) if ts <= now.to_f
|
||||
item.delete('at'.freeze) if ts <= now
|
||||
|
||||
client_push(item)
|
||||
end
|
||||
|
|
|
@ -11,6 +11,11 @@ class TestScheduling < Sidekiq::Test
|
|||
end
|
||||
end
|
||||
|
||||
# Assume we can pass any class as time to perform_in
|
||||
class TimeDuck
|
||||
def to_f; 42.0 end
|
||||
end
|
||||
|
||||
it 'schedules jobs' do
|
||||
ss = Sidekiq::ScheduledSet.new
|
||||
ss.clear
|
||||
|
@ -34,6 +39,9 @@ class TestScheduling < Sidekiq::Test
|
|||
|
||||
assert Sidekiq::Client.push_bulk('class' => ScheduledWorker, 'args' => [['mike'], ['mike']], 'at' => 600)
|
||||
assert_equal 5, ss.size
|
||||
|
||||
assert ScheduledWorker.perform_in(TimeDuck.new, 'samwise')
|
||||
assert_equal 6, ss.size
|
||||
end
|
||||
|
||||
it 'removes the enqueued_at field when scheduling' do
|
||||
|
|
Loading…
Reference in a new issue