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

Don't inherit from IceCube::Schedule.

This commit is contained in:
Tobias Svensson 2013-03-11 14:51:31 +00:00
parent 60b7db60e7
commit bdb6babe30
6 changed files with 51 additions and 19 deletions

View file

@ -7,6 +7,7 @@ HEAD
- Add `Sidetiq.retries`.
- Add `Sidetiq.logger`. This defaults to the Sidekiq logger.
- Clean up tests.
- Sidetiq::Schedule no longer inherits from IceCube::Schedule.
0.2.0
-----

View file

@ -11,10 +11,6 @@ module Sidetiq
include Singleton
include MonitorMixin
# Public: Start time offset from epoch used for calculating run
# times in the Sidetiq schedules.
START_TIME = Sidetiq.config.utc ? Time.utc(2010, 1, 1) : Time.local(2010, 1, 1)
# Internal: Returns a hash of Sidetiq::Schedule instances.
attr_reader :schedules
@ -41,7 +37,7 @@ module Sidetiq
#
# Returns a Sidetiq::Schedule instances.
def schedule_for(worker)
schedules[worker] ||= Sidetiq::Schedule.new(START_TIME)
schedules[worker] ||= Sidetiq::Schedule.new
end
# Public: Issue a single clock tick.

View file

@ -12,10 +12,12 @@ module Sidetiq
# end
module Schedulable
module ClassMethods
# Public: Returns a Float timestamp of the last scheduled run.
def last_scheduled_occurrence
get_timestamp "last"
end
# Public: Returns a Float timestamp of the next scheduled run.
def next_scheduled_occurrence
get_timestamp "next"
end
@ -41,3 +43,4 @@ module Sidetiq
end
end
end

View file

@ -1,23 +1,59 @@
module Sidetiq
# Internal: Recurrence schedules.
class Schedule < IceCube::Schedule
def method_missing(meth, *args, &block)
# Public: Recurrence schedule.
class Schedule
# Public: Start time offset from epoch used for calculating run
# times in the Sidetiq schedules.
START_TIME = Sidetiq.config.utc ? Time.utc(2010, 1, 1) : Time.local(2010, 1, 1)
def initialize # :nodoc:
@schedule = IceCube::Schedule.new(START_TIME)
end
def method_missing(meth, *args, &block) # :nodoc:
if IceCube::Rule.respond_to?(meth)
rule = IceCube::Rule.send(meth, *args, &block)
add_recurrence_rule(rule)
@schedule.add_recurrence_rule(rule)
rule
elsif @schedule.respond_to?(meth)
@schedule.send(meth, *args, &block)
else
super
end
end
# Public: Checks if a job is due to be scheduled.
#
# Returns true if a job is due, otherwise false.
def schedule_next?(time)
if @last_scheduled != (no = next_occurrence(time))
@last_scheduled = no
next_occurrence = @schedule.next_occurrence(time)
if @last_scheduled != next_occurrence
@last_scheduled = next_occurrence
return true
end
false
end
# Public: Schedule to String.
#
# Examples
#
# class MyWorker
# include Sidekiq::Worker
# include Sidetiq::Schedulable
#
# tiq { daily }
#
# def perform
# end
# end
#
# Sidetiq.schedules[MyWorker].to_s
# # => "Daily"
#
# Returns a String representing the schedule.
def to_s
@schedule.to_s
end
end
end

View file

@ -41,7 +41,7 @@ class TestClock < Sidetiq::TestCase
end
def test_enqueues_jobs_by_schedule
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule = Sidetiq::Schedule.new
schedule.daily
clock.stubs(:schedules).returns(SimpleWorker => schedule)
@ -60,7 +60,7 @@ class TestClock < Sidetiq::TestCase
end
def test_enqueues_jobs_with_default_last_tick_arg_on_first_run
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule = Sidetiq::Schedule.new
schedule.hourly
time = Time.local(2011, 1, 1, 1, 30)
@ -79,7 +79,7 @@ class TestClock < Sidetiq::TestCase
end
def test_enqueues_jobs_with_last_run_timestamp_and_next_run_timestamp
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule = Sidetiq::Schedule.new
schedule.hourly
time = Time.local(2011, 1, 1, 1, 30)
@ -98,7 +98,7 @@ class TestClock < Sidetiq::TestCase
end
def test_enqueues_jobs_correctly_for_splat_args_perform_methods
schedule = Sidetiq::Schedule.new(Sidetiq::Clock::START_TIME)
schedule = Sidetiq::Schedule.new
schedule.hourly
time = Time.local(2011, 1, 1, 1, 30)

View file

@ -1,10 +1,6 @@
require_relative 'helper'
class TestSchedule < Sidetiq::TestCase
def test_super
assert_equal IceCube::Schedule, Sidetiq::Schedule.superclass
end
def test_method_missing
sched = Sidetiq::Schedule.new
sched.daily