Class: Workers::Timer
- Inherits:
-
Object
show all
- Includes:
- Helpers
- Defined in:
- lib/workers/timer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#concat_e, #log_debug, #log_error, #log_info, #log_warn
Constructor Details
#initialize(delay, options = {}, &block) ⇒ Timer
Returns a new instance of Timer.
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/workers/timer.rb', line 8
def initialize(delay, options = {}, &block)
@logger = Workers::LogProxy.new(options[:logger])
@delay = delay
@callback = options[:callback] || block
@repeat = options[:repeat] || false
@scheduler = options[:scheduler] || Workers.scheduler
@mutex = Mutex.new
reset
@scheduler.schedule(self)
nil
end
|
Instance Attribute Details
#delay ⇒ Object
Returns the value of attribute delay.
5
6
7
|
# File 'lib/workers/timer.rb', line 5
def delay
@delay
end
|
#repeat ⇒ Object
Returns the value of attribute repeat.
6
7
8
|
# File 'lib/workers/timer.rb', line 6
def repeat
@repeat
end
|
Instance Method Details
#<=>(other) ⇒ Object
22
23
24
|
# File 'lib/workers/timer.rb', line 22
def <=>(other)
sec_remaining <=> other.sec_remaining
end
|
#cancel ⇒ Object
45
46
47
48
49
|
# File 'lib/workers/timer.rb', line 45
def cancel
@scheduler.unschedule(self)
nil
end
|
#fire ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/workers/timer.rb', line 37
def fire
@mutex.synchronize do
@callback.call if @callback
end
nil
end
|
#overdue? ⇒ Boolean
33
34
35
|
# File 'lib/workers/timer.rb', line 33
def overdue?
sec_remaining <= 0
end
|
#reset ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/workers/timer.rb', line 51
def reset
@mutex.synchronize do
@fire_at = Time.now.utc + @delay
end
nil
end
|
#sec_remaining ⇒ Object
26
27
28
29
30
31
|
# File 'lib/workers/timer.rb', line 26
def sec_remaining
@mutex.synchronize do
diff = @fire_at.to_f - Time.now.utc.to_f
(diff > 0) ? diff : 0
end
end
|