timers 4.1.1 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.rspec +1 -3
- data/.travis.yml +15 -14
- data/Gemfile +12 -3
- data/README.md +67 -21
- data/Rakefile +3 -4
- data/lib/timers/events.rb +109 -112
- data/lib/timers/group.rb +127 -127
- data/lib/timers/interval.rb +43 -0
- data/lib/timers/timer.rb +131 -125
- data/lib/timers/version.rb +8 -1
- data/lib/timers/wait.rb +48 -44
- data/lib/timers.rb +9 -3
- data/spec/spec_helper.rb +32 -13
- data/spec/timers/cancel_spec.rb +50 -0
- data/spec/timers/events_spec.rb +61 -0
- data/spec/timers/every_spec.rb +38 -0
- data/spec/timers/group_spec.rb +260 -0
- data/spec/timers/performance_spec.rb +125 -0
- data/spec/{strict_spec.rb → timers/strict_spec.rb} +12 -8
- data/spec/timers/wait_spec.rb +35 -0
- data/timers.gemspec +28 -18
- metadata +48 -49
- data/.coveralls.yml +0 -1
- data/AUTHORS.md +0 -15
- data/CHANGES.md +0 -54
- data/LICENSE +0 -23
- data/spec/cancel_spec.rb +0 -46
- data/spec/events_spec.rb +0 -57
- data/spec/every_spec.rb +0 -34
- data/spec/group_spec.rb +0 -254
- data/spec/performance_spec.rb +0 -100
- data/spec/timeout_spec.rb +0 -29
data/lib/timers/group.rb
CHANGED
|
@@ -1,132 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
|
4
|
+
#
|
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
|
6
|
+
#
|
|
1
7
|
|
|
2
|
-
require
|
|
3
|
-
require
|
|
4
|
-
require 'hitimes'
|
|
8
|
+
require "set"
|
|
9
|
+
require "forwardable"
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
require_relative "interval"
|
|
12
|
+
require_relative "timer"
|
|
13
|
+
require_relative "events"
|
|
8
14
|
|
|
9
15
|
module Timers
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
# The group's current time.
|
|
128
|
-
def current_offset
|
|
129
|
-
@interval.to_f
|
|
130
|
-
end
|
|
131
|
-
end
|
|
16
|
+
# A collection of timers which may fire at different times
|
|
17
|
+
class Group
|
|
18
|
+
include Enumerable
|
|
19
|
+
|
|
20
|
+
extend Forwardable
|
|
21
|
+
def_delegators :@timers, :each, :empty?
|
|
22
|
+
|
|
23
|
+
def initialize
|
|
24
|
+
@events = Events.new
|
|
25
|
+
|
|
26
|
+
@timers = Set.new
|
|
27
|
+
@paused_timers = Set.new
|
|
28
|
+
|
|
29
|
+
@interval = Interval.new
|
|
30
|
+
@interval.start
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Scheduled events:
|
|
34
|
+
attr_reader :events
|
|
35
|
+
|
|
36
|
+
# Active timers:
|
|
37
|
+
attr_reader :timers
|
|
38
|
+
|
|
39
|
+
# Paused timers:
|
|
40
|
+
attr_reader :paused_timers
|
|
41
|
+
|
|
42
|
+
# Call the given block after the given interval. The first argument will be
|
|
43
|
+
# the time at which the group was asked to fire timers for.
|
|
44
|
+
def after(interval, &block)
|
|
45
|
+
Timer.new(self, interval, false, &block)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Call the given block immediately, and then after the given interval. The first
|
|
49
|
+
# argument will be the time at which the group was asked to fire timers for.
|
|
50
|
+
def now_and_after(interval, &block)
|
|
51
|
+
yield
|
|
52
|
+
after(interval, &block)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Call the given block periodically at the given interval. The first
|
|
56
|
+
# argument will be the time at which the group was asked to fire timers for.
|
|
57
|
+
def every(interval, recur = true, &block)
|
|
58
|
+
Timer.new(self, interval, recur, &block)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Call the given block immediately, and then periodically at the given interval. The first
|
|
62
|
+
# argument will be the time at which the group was asked to fire timers for.
|
|
63
|
+
def now_and_every(interval, recur = true, &block)
|
|
64
|
+
yield
|
|
65
|
+
every(interval, recur, &block)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Wait for the next timer and fire it. Can take a block, which should behave
|
|
69
|
+
# like sleep(n), except that n may be nil (sleep forever) or a negative
|
|
70
|
+
# number (fire immediately after return).
|
|
71
|
+
def wait
|
|
72
|
+
if block_given?
|
|
73
|
+
yield wait_interval
|
|
74
|
+
|
|
75
|
+
while (interval = wait_interval) && interval > 0
|
|
76
|
+
yield interval
|
|
77
|
+
end
|
|
78
|
+
else
|
|
79
|
+
while (interval = wait_interval) && interval > 0
|
|
80
|
+
# We cannot assume that sleep will wait for the specified time, it might be +/- a bit.
|
|
81
|
+
sleep interval
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
fire
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Interval to wait until when the next timer will fire.
|
|
89
|
+
# - nil: no timers
|
|
90
|
+
# - -ve: timers expired already
|
|
91
|
+
# - 0: timers ready to fire
|
|
92
|
+
# - +ve: timers waiting to fire
|
|
93
|
+
def wait_interval(offset = current_offset)
|
|
94
|
+
handle = @events.first
|
|
95
|
+
handle.time - Float(offset) if handle
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Fire all timers that are ready.
|
|
99
|
+
def fire(offset = current_offset)
|
|
100
|
+
@events.fire(offset)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Pause all timers.
|
|
104
|
+
def pause
|
|
105
|
+
@timers.dup.each(&:pause)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Resume all timers.
|
|
109
|
+
def resume
|
|
110
|
+
@paused_timers.dup.each(&:resume)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
alias continue resume
|
|
114
|
+
|
|
115
|
+
# Delay all timers.
|
|
116
|
+
def delay(seconds)
|
|
117
|
+
@timers.each do |timer|
|
|
118
|
+
timer.delay(seconds)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Cancel all timers.
|
|
123
|
+
def cancel
|
|
124
|
+
@timers.dup.each(&:cancel)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# The group's current time.
|
|
128
|
+
def current_offset
|
|
129
|
+
@interval.to_f
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
132
|
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
|
4
|
+
#
|
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
|
6
|
+
#
|
|
7
|
+
|
|
8
|
+
module Timers
|
|
9
|
+
# A collection of timers which may fire at different times
|
|
10
|
+
class Interval
|
|
11
|
+
# Get the current elapsed monotonic time.
|
|
12
|
+
def initialize
|
|
13
|
+
@total = 0.0
|
|
14
|
+
@current = nil
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def start
|
|
18
|
+
return if @current
|
|
19
|
+
|
|
20
|
+
@current = now
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def stop
|
|
24
|
+
return unless @current
|
|
25
|
+
|
|
26
|
+
@total += duration
|
|
27
|
+
|
|
28
|
+
@current = nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_f
|
|
32
|
+
@total + duration
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
protected def duration
|
|
36
|
+
now - @current
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected def now
|
|
40
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
data/lib/timers/timer.rb
CHANGED
|
@@ -1,128 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
|
4
|
+
#
|
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
|
6
|
+
#
|
|
1
7
|
|
|
2
8
|
module Timers
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
9
|
+
# An individual timer set to fire a given proc at a given time. A timer is
|
|
10
|
+
# always connected to a Timer::Group but it would ONLY be in @group.timers
|
|
11
|
+
# if it also has a @handle specified. Otherwise it is either PAUSED or has
|
|
12
|
+
# been FIRED and is not recurring. You can manually enter this state by
|
|
13
|
+
# calling #cancel and resume normal operation by calling #reset.
|
|
14
|
+
class Timer
|
|
15
|
+
include Comparable
|
|
16
|
+
attr_reader :interval, :offset, :recurring
|
|
17
|
+
|
|
18
|
+
def initialize(group, interval, recurring = false, offset = nil, &block)
|
|
19
|
+
@group = group
|
|
20
|
+
|
|
21
|
+
@interval = interval
|
|
22
|
+
@recurring = recurring
|
|
23
|
+
@block = block
|
|
24
|
+
@offset = offset
|
|
25
|
+
|
|
26
|
+
@handle = nil
|
|
27
|
+
|
|
28
|
+
# If a start offset was supplied, use that, otherwise use the current timers offset.
|
|
29
|
+
reset(@offset || @group.current_offset)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def paused?
|
|
33
|
+
@group.paused_timers.include? self
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def pause
|
|
37
|
+
return if paused?
|
|
38
|
+
|
|
39
|
+
@group.timers.delete self
|
|
40
|
+
@group.paused_timers.add self
|
|
41
|
+
|
|
42
|
+
@handle.cancel! if @handle
|
|
43
|
+
@handle = nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def resume
|
|
47
|
+
return unless paused?
|
|
48
|
+
|
|
49
|
+
@group.paused_timers.delete self
|
|
50
|
+
|
|
51
|
+
# This will add us back to the group:
|
|
52
|
+
reset
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
alias continue resume
|
|
56
|
+
|
|
57
|
+
# Extend this timer
|
|
58
|
+
def delay(seconds)
|
|
59
|
+
@handle.cancel! if @handle
|
|
60
|
+
|
|
61
|
+
@offset += seconds
|
|
62
|
+
|
|
63
|
+
@handle = @group.events.schedule(@offset, self)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Cancel this timer. Do not call while paused.
|
|
67
|
+
def cancel
|
|
68
|
+
return unless @handle
|
|
69
|
+
|
|
70
|
+
@handle.cancel! if @handle
|
|
71
|
+
@handle = nil
|
|
72
|
+
|
|
73
|
+
# This timer is no longer valid:
|
|
74
|
+
@group.timers.delete self if @group
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Reset this timer. Do not call while paused.
|
|
78
|
+
def reset(offset = @group.current_offset)
|
|
79
|
+
# This logic allows us to minimise the interaction with @group.timers.
|
|
80
|
+
# A timer with a handle is always registered with the group.
|
|
81
|
+
if @handle
|
|
82
|
+
@handle.cancel!
|
|
83
|
+
else
|
|
84
|
+
@group.timers << self
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
@offset = Float(offset) + @interval
|
|
88
|
+
|
|
89
|
+
@handle = @group.events.schedule(@offset, self)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Fire the block.
|
|
93
|
+
def fire(offset = @group.current_offset)
|
|
94
|
+
if recurring == :strict
|
|
95
|
+
# ... make the next interval strictly the last offset + the interval:
|
|
96
|
+
reset(@offset)
|
|
97
|
+
elsif recurring
|
|
98
|
+
reset(offset)
|
|
99
|
+
else
|
|
100
|
+
@offset = offset
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
@block.call(offset)
|
|
104
|
+
|
|
105
|
+
cancel unless recurring
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
alias call fire
|
|
109
|
+
|
|
110
|
+
# Number of seconds until next fire / since last fire
|
|
111
|
+
def fires_in
|
|
112
|
+
@offset - @group.current_offset if @offset
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Inspect a timer
|
|
116
|
+
def inspect
|
|
117
|
+
str = "#{to_s[0..-2]} ".dup
|
|
118
|
+
|
|
119
|
+
if @offset
|
|
120
|
+
str << if fires_in >= 0
|
|
121
|
+
"fires in #{fires_in} seconds"
|
|
122
|
+
else
|
|
123
|
+
"fired #{fires_in.abs} seconds ago"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
str << ", recurs every #{interval}" if recurring
|
|
127
|
+
else
|
|
128
|
+
str << "dead"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
str << ">"
|
|
132
|
+
end
|
|
133
|
+
end
|
|
128
134
|
end
|
data/lib/timers/version.rb
CHANGED
data/lib/timers/wait.rb
CHANGED
|
@@ -1,48 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
#
|
|
3
|
+
# This file is part of the "timers" project and released under the MIT license.
|
|
4
|
+
#
|
|
5
|
+
# Copyright, 2018, by Samuel Williams. All rights reserved.
|
|
6
|
+
#
|
|
1
7
|
|
|
2
|
-
|
|
8
|
+
require_relative "interval"
|
|
3
9
|
|
|
4
10
|
module Timers
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
end
|
|
47
|
-
end
|
|
11
|
+
# An exclusive, monotonic timeout class.
|
|
12
|
+
class Wait
|
|
13
|
+
def self.for(duration, &block)
|
|
14
|
+
if duration
|
|
15
|
+
timeout = new(duration)
|
|
16
|
+
|
|
17
|
+
timeout.while_time_remaining(&block)
|
|
18
|
+
else
|
|
19
|
+
loop do
|
|
20
|
+
yield(nil)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(duration)
|
|
26
|
+
@duration = duration
|
|
27
|
+
@remaining = true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
attr_reader :duration
|
|
31
|
+
attr_reader :remaining
|
|
32
|
+
|
|
33
|
+
# Yields while time remains for work to be done:
|
|
34
|
+
def while_time_remaining
|
|
35
|
+
@interval = Interval.new
|
|
36
|
+
@interval.start
|
|
37
|
+
|
|
38
|
+
yield @remaining while time_remaining?
|
|
39
|
+
ensure
|
|
40
|
+
@interval.stop
|
|
41
|
+
@interval = nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def time_remaining?
|
|
47
|
+
@remaining = (@duration - @interval.to_f)
|
|
48
|
+
|
|
49
|
+
@remaining > 0
|
|
50
|
+
end
|
|
51
|
+
end
|
|
48
52
|
end
|