mirror of
https://github.com/endofunky/sidetiq.git
synced 2022-11-09 13:53:30 -05:00
add class methods to get last/next run timestamps for a worker
This commit is contained in:
parent
26600bef23
commit
91e90f418e
2 changed files with 41 additions and 1 deletions
|
@ -12,12 +12,28 @@ module Sidetiq
|
|||
# end
|
||||
module Schedulable
|
||||
module ClassMethods
|
||||
def last_scheduled_occurrence
|
||||
get_timestamp "last"
|
||||
end
|
||||
|
||||
def next_scheduled_occurrence
|
||||
get_timestamp "next"
|
||||
end
|
||||
|
||||
def tiq(&block) # :nodoc:
|
||||
clock = Sidetiq::Clock.instance
|
||||
clock.synchronize do
|
||||
clock.schedule_for(self).instance_eval(&block)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_timestamp key
|
||||
Sidekiq.redis do |redis|
|
||||
(redis.get("sidetiq:#{name}:#{key}") || -1).to_f
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(klass) # :nodoc:
|
||||
|
@ -25,4 +41,3 @@ module Sidetiq
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
25
test/test_worker.rb
Normal file
25
test/test_worker.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
require_relative 'helper'
|
||||
|
||||
class TestWorker < Sidetiq::TestCase
|
||||
class FakeWorker
|
||||
include Sidetiq::Schedulable
|
||||
end
|
||||
|
||||
def test_timestamps_for_new_worker
|
||||
assert FakeWorker.last_scheduled_occurrence == -1
|
||||
assert FakeWorker.next_scheduled_occurrence == -1
|
||||
end
|
||||
|
||||
def test_timestamps_for_existing_worker
|
||||
last_run = (Time.now - 100).to_f
|
||||
next_run = (Time.now + 100).to_f
|
||||
|
||||
Sidekiq.redis do |redis|
|
||||
redis.set "sidetiq:TestWorker::FakeWorker:last", last_run
|
||||
redis.set "sidetiq:TestWorker::FakeWorker:next", next_run
|
||||
end
|
||||
|
||||
assert FakeWorker.last_scheduled_occurrence == last_run
|
||||
assert FakeWorker.next_scheduled_occurrence == next_run
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue