mirror of
https://github.com/endofunky/sidetiq.git
synced 2022-11-09 13:53:30 -05:00
Support manual starting/stopping of the clock.
This commit is contained in:
parent
61effa89a8
commit
f4c81098f1
2 changed files with 40 additions and 8 deletions
|
@ -12,7 +12,7 @@ module Sidetiq
|
||||||
|
|
||||||
START_TIME = Sidetiq.config.utc ? Time.utc(2010, 1, 1) : Time.local(2010, 1, 1)
|
START_TIME = Sidetiq.config.utc ? Time.utc(2010, 1, 1) : Time.local(2010, 1, 1)
|
||||||
|
|
||||||
attr_reader :schedules
|
attr_reader :schedules, :thread
|
||||||
|
|
||||||
def self.method_missing(meth, *args, &block)
|
def self.method_missing(meth, *args, &block)
|
||||||
instance.__send__(meth, *args, &block)
|
instance.__send__(meth, *args, &block)
|
||||||
|
@ -42,6 +42,26 @@ module Sidetiq
|
||||||
Sidetiq.config.utc ? clock_gettime.utc : clock_gettime
|
Sidetiq.config.utc ? clock_gettime.utc : clock_gettime
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def start!
|
||||||
|
return if ticking?
|
||||||
|
|
||||||
|
Sidekiq.logger.info "Sidetiq::Clock start"
|
||||||
|
@thread = Thread.start { clock { tick } }
|
||||||
|
@thread.abort_on_exception = true
|
||||||
|
@thread.priority = Sidetiq.config.resolution
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop!
|
||||||
|
if ticking?
|
||||||
|
@thread.kill
|
||||||
|
Sidekiq.logger.info "Sidetiq::Clock stop"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def ticking?
|
||||||
|
@thread && @thread.alive?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def enqueue(worker, time)
|
def enqueue(worker, time)
|
||||||
|
@ -73,13 +93,6 @@ module Sidetiq
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start!
|
|
||||||
Sidekiq.logger.info "Sidetiq::Clock start"
|
|
||||||
thr = Thread.start { clock { tick } }
|
|
||||||
thr.abort_on_exception = true
|
|
||||||
thr.priority = Sidetiq.config.resolution
|
|
||||||
end
|
|
||||||
|
|
||||||
def clock
|
def clock
|
||||||
loop do
|
loop do
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -9,6 +9,25 @@ class TestClock < Sidetiq::TestCase
|
||||||
Sidetiq::Clock.foo
|
Sidetiq::Clock.foo
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_start_stop
|
||||||
|
refute clock.ticking?
|
||||||
|
assert_nil clock.thread
|
||||||
|
|
||||||
|
clock.start!
|
||||||
|
Thread.pass
|
||||||
|
sleep 0.01
|
||||||
|
|
||||||
|
assert clock.ticking?
|
||||||
|
assert_kind_of Thread, clock.thread
|
||||||
|
|
||||||
|
clock.stop!
|
||||||
|
Thread.pass
|
||||||
|
sleep 0.01
|
||||||
|
|
||||||
|
refute clock.ticking?
|
||||||
|
refute clock.thread.alive?
|
||||||
|
end
|
||||||
|
|
||||||
def test_gettime_seconds
|
def test_gettime_seconds
|
||||||
assert_equal clock.gettime.tv_sec, Time.now.tv_sec
|
assert_equal clock.gettime.tv_sec, Time.now.tv_sec
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue