1
0
Fork 0
mirror of https://github.com/endofunky/sidetiq.git synced 2022-11-09 13:53:30 -05:00

send last and scheduled invocation timestamps to worker.perform, if required

This commit is contained in:
Lee Henson 2013-03-08 15:08:19 +00:00
parent 342e4f0a94
commit 26600bef23
2 changed files with 82 additions and 9 deletions

View file

@ -131,15 +131,20 @@ module Sidetiq
def enqueue(worker, time)
key = "sidetiq:#{worker.name}"
time_f = time.to_f
synchronize_clockworks("#{key}:lock") do |redis|
status = redis.get(key)
next_run = (redis.get("#{key}:next") || -1).to_f
if status.nil? || status.to_f < time.to_f
time_f = time.to_f
Sidekiq.logger.info "Sidetiq::Clock enqueue #{worker.name} (at: #{time_f})"
redis.set(key, time_f)
worker.perform_at(time)
if next_run < time_f
Sidekiq.logger.info "Sidetiq::Clock enqueue #{worker.name} (at: #{time_f}) (last: #{next_run})"
redis.mset("#{key}:last", next_run, "#{key}:next", time_f)
arity = [worker.instance_method(:perform).arity - 1, -1].max
args = [next_run, time_f][0..arity]
worker.perform_at(time, *args)
end
end
end
@ -176,4 +181,3 @@ module Sidetiq
end
end
end

View file

@ -1,7 +1,9 @@
require_relative 'helper'
class TestClock < Sidetiq::TestCase
class FakeWorker;
class FakeWorker
def perform
end
end
def test_delegates_to_instance
@ -61,5 +63,72 @@ class TestClock < Sidetiq::TestCase
clock.tick
clock.tick
end
end
class LastTickWorker
def perform last_tick
end
end
def test_enqueues_jobs_with_default_last_tick_arg_on_first_run
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule.hourly
time = Time.local(2011, 1, 1, 1, 30)
clock.stubs(:gettime).returns(time, time + 3600)
clock.stubs(:schedules).returns(LastTickWorker => schedule)
expected_first_tick = time + 1800
expected_second_tick = expected_first_tick + 3600
LastTickWorker.expects(:perform_at).with(expected_first_tick, -1).once
LastTickWorker.expects(:perform_at).with(expected_second_tick, expected_first_tick.to_f).once
clock.tick
clock.tick
end
class LastAndScheduledTicksWorker
def perform last_tick, scheduled_tick
end
end
def test_enqueues_jobs_with_last_run_timestamp_and_next_run_timestamp
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule.hourly
time = Time.local(2011, 1, 1, 1, 30)
clock.stubs(:gettime).returns(time, time + 3600)
clock.stubs(:schedules).returns(LastAndScheduledTicksWorker => schedule)
expected_first_tick = time + 1800
expected_second_tick = expected_first_tick + 3600
LastAndScheduledTicksWorker.expects(:perform_at).with(expected_first_tick, -1, expected_first_tick.to_f).once
clock.tick
LastAndScheduledTicksWorker.expects(:perform_at).with(expected_second_tick, expected_first_tick.to_f, expected_second_tick.to_f).once
clock.tick
end
class SplatArgsWorker
def perform arg1, *args
end
end
def test_enqueues_jobs_correctly_for_splat_args_perform_methods
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule.hourly
time = Time.local(2011, 1, 1, 1, 30)
clock.stubs(:gettime).returns(time, time + 3600)
clock.stubs(:schedules).returns(SplatArgsWorker => schedule)
expected_first_tick = time + 1800
SplatArgsWorker.expects(:perform_at).with(expected_first_tick, -1, expected_first_tick.to_f).once
clock.tick
end
end