diff --git a/lib/sidetiq.rb b/lib/sidetiq.rb index 17e7db9..3e4a370 100644 --- a/lib/sidetiq.rb +++ b/lib/sidetiq.rb @@ -31,5 +31,55 @@ module Sidetiq Clock.schedules.dup end end + + # Public: Currently scheduled recurring jobs. + # + # worker - A Sidekiq::Worker class or String of the class name (optional) + # block - An optional block that can be given to which each + # Sidekiq::SortedEntry instance corresponding to a scheduled job will + # be yielded. + # + # Examples + # + # Sidetiq.scheduled + # # => [#, ...] + # + # Sidetiq.scheduled(MyWorker) + # # => [#, ...] + # + # Sidetiq.scheduled("MyWorker") + # # => [#, ...] + # + # Sidetiq.scheduled do |job| + # # do stuff ... + # end + # # => [#, ...] + # + # Sidetiq.scheduled(MyWorker) do |job| + # # do stuff ... + # end + # # => [#, ...] + # + # Sidetiq.scheduled("MyWorker") do |job| + # # do stuff ... + # end + # # => [#, ...] + # + # Yields each Sidekiq::SortedEntry instance. + # Returns an Array of Sidekiq::SortedEntry objects. + def self.scheduled(worker = nil, &block) + worker = worker.constantize if worker.kind_of?(String) + + scheduled = Sidekiq::ScheduledSet.new.select do |job| + klass = job.klass.constantize + ret = klass.include?(Schedulable) + ret = ret && klass == worker if worker + ret + end + + scheduled.each(&block) if block_given? + + scheduled + end end diff --git a/test/test_sidetiq.rb b/test/test_sidetiq.rb index 009ae9e..39bc551 100644 --- a/test/test_sidetiq.rb +++ b/test/test_sidetiq.rb @@ -1,6 +1,10 @@ require_relative 'helper' class TestSidetiq < Sidetiq::TestCase + def teardown + Sidekiq::ScheduledSet.new.map(&:delete) + end + def test_schedules assert_equal Sidetiq.schedules, Sidetiq::Clock.schedules assert_equal [ScheduledWorker], Sidetiq.schedules.keys @@ -10,5 +14,53 @@ class TestSidetiq < Sidetiq::TestCase def test_workers assert_equal [ScheduledWorker], Sidetiq.workers end + + def test_scheduled + SimpleWorker.perform_at(Time.local(2011, 1, 1, 1)) + SimpleWorker.client_push_old(SimpleWorker.jobs.first) + + scheduled = Sidetiq.scheduled + + assert_kind_of Array, scheduled + assert_kind_of Sidekiq::SortedEntry, scheduled.first + assert_equal 1, scheduled.length + end + + def test_scheduled_given_arguments + SimpleWorker.perform_at(Time.local(2011, 1, 1, 1)) + SimpleWorker.client_push_old(SimpleWorker.jobs.first) + + assert_equal 1, Sidetiq.scheduled(SimpleWorker).length + assert_equal 0, Sidetiq.scheduled(ScheduledWorker).length + + assert_equal 1, Sidetiq.scheduled("SimpleWorker").length + assert_equal 0, Sidetiq.scheduled("ScheduledWorker").length + end + + def test_scheduled_yields_each_job + SimpleWorker.perform_at(Time.local(2011, 1, 1, 1)) + SimpleWorker.client_push_old(SimpleWorker.jobs.first) + + ScheduledWorker.perform_at(Time.local(2011, 1, 1, 1)) + ScheduledWorker.client_push_old(ScheduledWorker.jobs.first) + + jobs = [] + Sidetiq.scheduled { |job| jobs << job } + assert_equal 2, jobs.length + + jobs = [] + Sidetiq.scheduled(SimpleWorker) { |job| jobs << job } + assert_equal 1, jobs.length + + jobs = [] + Sidetiq.scheduled("ScheduledWorker") { |job| jobs << job } + assert_equal 1, jobs.length + end + + def test_scheduled_with_invalid_class + assert_raises(NameError) do + Sidetiq.scheduled("Foobar") + end + end end