diff --git a/CHANGELOG.md b/CHANGELOG.md index 2daff5b..bfb18c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/lib/sidetiq/clock.rb b/lib/sidetiq/clock.rb index ba5dcab..c8a1295 100644 --- a/lib/sidetiq/clock.rb +++ b/lib/sidetiq/clock.rb @@ -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. diff --git a/lib/sidetiq/schedulable.rb b/lib/sidetiq/schedulable.rb index 4519216..e397f23 100644 --- a/lib/sidetiq/schedulable.rb +++ b/lib/sidetiq/schedulable.rb @@ -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 + diff --git a/lib/sidetiq/schedule.rb b/lib/sidetiq/schedule.rb index 316ad39..4e09e51 100644 --- a/lib/sidetiq/schedule.rb +++ b/lib/sidetiq/schedule.rb @@ -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 diff --git a/test/test_clock.rb b/test/test_clock.rb index 3f38dd4..e2a0cc2 100644 --- a/test/test_clock.rb +++ b/test/test_clock.rb @@ -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) diff --git a/test/test_schedule.rb b/test/test_schedule.rb index 4880233..6526c4a 100644 --- a/test/test_schedule.rb +++ b/test/test_schedule.rb @@ -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