mirror of
https://github.com/endofunky/sidetiq.git
synced 2022-11-09 13:53:30 -05:00
Add Sidetiq.scheduled.
This commit is contained in:
parent
4b0a71ff51
commit
486f45adb3
2 changed files with 102 additions and 0 deletions
|
@ -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
|
||||
# # => [#<Sidekiq::SortedEntry>, ...]
|
||||
#
|
||||
# Sidetiq.scheduled(MyWorker)
|
||||
# # => [#<Sidekiq::SortedEntry>, ...]
|
||||
#
|
||||
# Sidetiq.scheduled("MyWorker")
|
||||
# # => [#<Sidekiq::SortedEntry>, ...]
|
||||
#
|
||||
# Sidetiq.scheduled do |job|
|
||||
# # do stuff ...
|
||||
# end
|
||||
# # => [#<Sidekiq::SortedEntry>, ...]
|
||||
#
|
||||
# Sidetiq.scheduled(MyWorker) do |job|
|
||||
# # do stuff ...
|
||||
# end
|
||||
# # => [#<Sidekiq::SortedEntry>, ...]
|
||||
#
|
||||
# Sidetiq.scheduled("MyWorker") do |job|
|
||||
# # do stuff ...
|
||||
# end
|
||||
# # => [#<Sidekiq::SortedEntry>, ...]
|
||||
#
|
||||
# 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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue