actiontimer 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +16 -0
- data/LICENSE +165 -0
- data/README.rdoc +111 -0
- data/actiontimer.gemspec +17 -0
- data/lib/actiontimer.rb +1 -0
- data/lib/actiontimer/Action.rb +73 -0
- data/lib/actiontimer/Exceptions.rb +8 -0
- data/lib/actiontimer/Timer.rb +177 -0
- data/test +11 -0
- data/tests/run_tests.rb +145 -0
- metadata +81 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
= CHANGELOG
|
2
|
+
|
3
|
+
== 0.1.1
|
4
|
+
* fix for float calculations
|
5
|
+
* downcased gem name
|
6
|
+
|
7
|
+
== 0.1.0
|
8
|
+
* remove excess exceptions
|
9
|
+
* use logger directly
|
10
|
+
* add a splat for data passage
|
11
|
+
|
12
|
+
== 0.0.2
|
13
|
+
* added new Timer::running? method
|
14
|
+
* added new Timer::pause method
|
15
|
+
* fixed single iteration action bug
|
16
|
+
* added unit tests for proper checks
|
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.rdoc
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
== ActionTimer: Simple timing for a complex world
|
2
|
+
|
3
|
+
ActionTimer is a helper for timed events. It allows for single and recurring actions to be executed in an efficient manner. It makes use of a single thread to keep time on registered actions and uses an ActionPool to execute actions. Simple and effective.
|
4
|
+
|
5
|
+
|
6
|
+
=== install (easy):
|
7
|
+
|
8
|
+
gem install actiontimer
|
9
|
+
|
10
|
+
=== install (less easy):
|
11
|
+
|
12
|
+
git clone http://github.com/spox/actiontimer.git
|
13
|
+
cd actiontimer && gem build *.gemspec && gem install ./
|
14
|
+
|
15
|
+
=== install (less easy that's a little easier)
|
16
|
+
|
17
|
+
{rip}[http://hellorip.com/about.html] makes it easy to install directly from a github repository.
|
18
|
+
|
19
|
+
=== Using the timer:
|
20
|
+
|
21
|
+
==== Simple example:
|
22
|
+
|
23
|
+
require 'actiontimer'
|
24
|
+
timer = ActionTimer::Timer.new
|
25
|
+
timer.add(1){ puts "#{Time.now}: This is timed every 1 second." }
|
26
|
+
timer.add(2){ puts "#{Time.now}: This is timed every 2 seconds." }
|
27
|
+
loop do
|
28
|
+
puts "#{Time.now}: Main loop sleeps for 3 seconds."
|
29
|
+
sleep(3)
|
30
|
+
end
|
31
|
+
|
32
|
+
=>
|
33
|
+
2010-01-05 17:52:46 -0800: Main loop sleeps for 3 seconds.
|
34
|
+
2010-01-05 17:52:47 -0800: This is timed every 1 second.
|
35
|
+
2010-01-05 17:52:48 -0800: This is timed every 1 second.
|
36
|
+
2010-01-05 17:52:48 -0800: This is timed every 2 seconds.
|
37
|
+
2010-01-05 17:52:49 -0800: Main loop sleeps for 3 seconds.
|
38
|
+
2010-01-05 17:52:49 -0800: This is timed every 1 second.
|
39
|
+
2010-01-05 17:52:50 -0800: This is timed every 1 second.
|
40
|
+
2010-01-05 17:52:50 -0800: This is timed every 2 seconds.
|
41
|
+
2010-01-05 17:52:51 -0800: This is timed every 1 second.
|
42
|
+
2010-01-05 17:52:52 -0800: Main loop sleeps for 3 seconds.
|
43
|
+
|
44
|
+
==== Other examples:
|
45
|
+
|
46
|
+
What if you want to sleep for less than a second? Well, sure we can do that:
|
47
|
+
|
48
|
+
require 'actiontimer'
|
49
|
+
result = 0
|
50
|
+
timer = ActionTimer::Timer.new
|
51
|
+
timer.add(0.1){ result += 1 }
|
52
|
+
sleep(1.01)
|
53
|
+
p result
|
54
|
+
|
55
|
+
=> 10
|
56
|
+
|
57
|
+
How about passing data to your block:
|
58
|
+
|
59
|
+
require 'actiontimer'
|
60
|
+
data = :foobar
|
61
|
+
timer = ActionTimer::Timer.new
|
62
|
+
timer.add(0.01, false, data){|x| puts "Data: #{x}" }
|
63
|
+
data = :fubar
|
64
|
+
p data
|
65
|
+
sleep(0.011)
|
66
|
+
p data
|
67
|
+
|
68
|
+
=>
|
69
|
+
:fubar
|
70
|
+
Data: foobar
|
71
|
+
:fubar
|
72
|
+
|
73
|
+
Or maybe you don't want the timer to start right away:
|
74
|
+
|
75
|
+
require 'actiontimer'
|
76
|
+
timer = ActionTimer::Timer.new(:auto_start => false)
|
77
|
+
output = 0
|
78
|
+
timer.add(0.1){ output += 1 }
|
79
|
+
sleep(1)
|
80
|
+
p output
|
81
|
+
timer.start
|
82
|
+
sleep(1.01)
|
83
|
+
p output
|
84
|
+
|
85
|
+
=>
|
86
|
+
0
|
87
|
+
10
|
88
|
+
|
89
|
+
What if you want to add multiple actions at one time? We can do this:
|
90
|
+
|
91
|
+
require 'actiontimer'
|
92
|
+
timer = ActionTimer::Timer.new
|
93
|
+
result = 0
|
94
|
+
actions = []
|
95
|
+
actions << ActionTimer::Action.new(@timer, 0.1){ result += 1}
|
96
|
+
actions << ActionTimer::Action.new(@timer, 0.2){ result += 1}
|
97
|
+
actions << ActionTimer::Action.new(@timer, 0.3){ result += 1}
|
98
|
+
timer.mass_add(actions)
|
99
|
+
sleep(0.41)
|
100
|
+
p result
|
101
|
+
|
102
|
+
=> 7
|
103
|
+
|
104
|
+
== Last remarks
|
105
|
+
|
106
|
+
If you find any bugs, please report them through {github}[http://github.com/spox/actiontimer/issues]. If you are in need of any help, you can generally find me on DALnet and Freenode.
|
107
|
+
|
108
|
+
== License
|
109
|
+
|
110
|
+
ActionPool is licensed under the LGPLv3
|
111
|
+
Copyright (c) 2009 spox <spox@modspox.com>
|
data/actiontimer.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = 'actiontimer'
|
3
|
+
s.author = 'spox'
|
4
|
+
s.email = 'spox@modspox.com'
|
5
|
+
s.version = '0.1.1'
|
6
|
+
s.summary = 'Simple timer for a complex world'
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.has_rdoc = true
|
9
|
+
s.rdoc_options = %w(--title ActionTimer --main README.rdoc --line-numbers --inline-source)
|
10
|
+
s.extra_rdoc_files = %w(README.rdoc LICENSE CHANGELOG)
|
11
|
+
s.files = Dir['**/*']
|
12
|
+
s.require_paths = %w(lib)
|
13
|
+
s.add_dependency 'ActionPool'
|
14
|
+
s.required_ruby_version = '>= 1.8.6'
|
15
|
+
s.homepage = 'http://github.com/spox/actiontimer'
|
16
|
+
s.description = 'ActionTimer is a simple timer for recurring actions. It supports single and recurring actions with an easy to use API.'
|
17
|
+
end
|
data/lib/actiontimer.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'actiontimer/Timer.rb'
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module ActionTimer
|
2
|
+
class Action
|
3
|
+
|
4
|
+
attr_accessor :owner
|
5
|
+
|
6
|
+
# timer:: Timer this action resides within
|
7
|
+
# period:: amount of time between runs
|
8
|
+
# once:: only run this action once
|
9
|
+
# data:: data to pass to block
|
10
|
+
# block:: block to be executed
|
11
|
+
def initialize(timer, period, once=false, data=nil, &block)
|
12
|
+
@period = period.to_f
|
13
|
+
@block = block
|
14
|
+
@data = data
|
15
|
+
@once = once
|
16
|
+
@timer = timer
|
17
|
+
@completed = false
|
18
|
+
@wait_remaining = @period
|
19
|
+
@owner = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
# o:: Object that added this action
|
23
|
+
# Adds an owner for this action. Useful
|
24
|
+
# for clearing all actions for a given
|
25
|
+
# object from the timer
|
26
|
+
def owner=(o)
|
27
|
+
@owner = o
|
28
|
+
end
|
29
|
+
|
30
|
+
# amount:: amount of time that has passed
|
31
|
+
# Decrement remaining wait time by given amount
|
32
|
+
def tick(amount)
|
33
|
+
@wait_remaining = @wait_remaining - amount if @wait_remaining > 0
|
34
|
+
@wait_remaining = 0 if @wait_remaining < 0
|
35
|
+
@completed = true if @once && @wait_remaining <= 0
|
36
|
+
end
|
37
|
+
|
38
|
+
# Time remaning before Action is due
|
39
|
+
def remaining
|
40
|
+
@wait_remaining <= 0 ? 0 : @wait_remaining
|
41
|
+
end
|
42
|
+
|
43
|
+
# new_time:: new period
|
44
|
+
# Resets the wait period between runs
|
45
|
+
def reset_period(new_time)
|
46
|
+
@period = new_time.to_f
|
47
|
+
@wait_remaining = @period
|
48
|
+
@timer.wakeup
|
49
|
+
end
|
50
|
+
|
51
|
+
# Action is ready to be destroyed
|
52
|
+
def is_complete?
|
53
|
+
@completed
|
54
|
+
end
|
55
|
+
|
56
|
+
# Used for scheduling with Timer. Resets the interval
|
57
|
+
# and returns itself
|
58
|
+
def schedule
|
59
|
+
@wait_remaining = @period
|
60
|
+
return self
|
61
|
+
end
|
62
|
+
|
63
|
+
# Is action due for execution
|
64
|
+
def due?
|
65
|
+
@wait_remaining <= 0
|
66
|
+
end
|
67
|
+
|
68
|
+
# Run the action
|
69
|
+
def run
|
70
|
+
@data.nil? ? @block.call : @block.call(*@data)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'actionpool'
|
2
|
+
['Action', 'Exceptions'].each{|f| require "actiontimer/#{f}"}
|
3
|
+
|
4
|
+
module ActionTimer
|
5
|
+
class Timer
|
6
|
+
# pool:: ActionPool for processing actions
|
7
|
+
# Creates a new timer
|
8
|
+
# Argument hash: {:pool, :logger, :auto_start}
|
9
|
+
def initialize(args={}, extra=nil)
|
10
|
+
auto_start = true
|
11
|
+
if(args.is_a?(Hash))
|
12
|
+
@pool = args[:pool] ? args[:pool] : ActionPool::Pool.new
|
13
|
+
@logger = args[:logger] && args[:logger].is_a?(Logger) ? args[:logger] : Logger.new(nil)
|
14
|
+
auto_start = args.has_key?(:auto_start) ? args[:auto_start] : true
|
15
|
+
else
|
16
|
+
@pool = args.is_a?(ActionPool::Pool) ? args : ActionPool::Pool.new
|
17
|
+
@logger = extra && extra.is_a?(Logger) ? extra : Logger.new(nil)
|
18
|
+
end
|
19
|
+
@actions = []
|
20
|
+
@new_actions = []
|
21
|
+
@timer_thread = nil
|
22
|
+
@stop_timer = false
|
23
|
+
@add_lock = Mutex.new
|
24
|
+
@awake_lock = Mutex.new
|
25
|
+
start if auto_start
|
26
|
+
end
|
27
|
+
|
28
|
+
# Forcibly wakes the timer early
|
29
|
+
def wakeup
|
30
|
+
raise NotRunning.new unless running?
|
31
|
+
return unless @awake_lock.try_lock
|
32
|
+
@timer_thread.wakeup if @timer_thread.status == 'sleep'
|
33
|
+
@awake_lock.unlock
|
34
|
+
end
|
35
|
+
|
36
|
+
# period:: amount of time between runs
|
37
|
+
# once:: only run this action once
|
38
|
+
# data:: data to pass to block
|
39
|
+
# owner:: owner of Action
|
40
|
+
# func:: block to be executed
|
41
|
+
# Add a new action to block
|
42
|
+
def add(period, once=false, data=nil, owner=nil, &func)
|
43
|
+
action = Action.new(self, period, once, data, &func)
|
44
|
+
action.owner = owner unless owner.nil?
|
45
|
+
@add_lock.synchronize{ @new_actions << action }
|
46
|
+
wakeup if running?
|
47
|
+
return action
|
48
|
+
end
|
49
|
+
|
50
|
+
# actions:: Array of actions
|
51
|
+
# Add multiple Actions to the timer at once
|
52
|
+
def mass_add(actions)
|
53
|
+
raise ArgumentError.new('Expecting an array') unless actions.is_a?(Array)
|
54
|
+
actions.each do |action|
|
55
|
+
raise ArgumentError.new('Expecting an Action') unless action.is_a?(Action)
|
56
|
+
end
|
57
|
+
@add_lock.synchronize{ @new_actions = @new_actions + actions }
|
58
|
+
wakeup if running?
|
59
|
+
end
|
60
|
+
|
61
|
+
# action:: Action to remove from timer
|
62
|
+
# Remove given action from timer
|
63
|
+
def remove(action)
|
64
|
+
raise ArgumentError.new('Expecting an action') unless action.is_a?(Action)
|
65
|
+
@actions.delete(action)
|
66
|
+
wakeup if running?
|
67
|
+
end
|
68
|
+
|
69
|
+
# Start the timer
|
70
|
+
def start
|
71
|
+
raise AlreadyRunning.new unless @timer_thread.nil?
|
72
|
+
@stop_timer = false
|
73
|
+
@timer_thread = Thread.new do
|
74
|
+
begin
|
75
|
+
until @stop_timer do
|
76
|
+
to_sleep = get_min_sleep
|
77
|
+
if((to_sleep.nil? || to_sleep > 0) && @new_actions.empty?)
|
78
|
+
@awake_lock.unlock if @awake_lock.locked?
|
79
|
+
actual_sleep = to_sleep.nil? ? sleep : sleep(to_sleep)
|
80
|
+
@awake_lock.lock
|
81
|
+
else
|
82
|
+
actual_sleep = 0
|
83
|
+
end
|
84
|
+
actual_sleep = to_sleep if actual_sleep <= 0
|
85
|
+
tick(actual_sleep)
|
86
|
+
add_waiting_actions
|
87
|
+
end
|
88
|
+
rescue Object => boom
|
89
|
+
@timer_thread = nil
|
90
|
+
clean_actions
|
91
|
+
@logger.fatal("Timer encountered an unexpected error: #{boom}\n#{boom.backtrace.join("\n")}")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Pause the timer in its current state.
|
97
|
+
def pause
|
98
|
+
@stop_timer = true
|
99
|
+
if(running?)
|
100
|
+
wakeup
|
101
|
+
@timer_thread.join
|
102
|
+
end
|
103
|
+
@timer_thread = nil
|
104
|
+
end
|
105
|
+
|
106
|
+
# Stop the timer. Unlike pause, this will completely
|
107
|
+
# stop the timer and remove all actions from the timer
|
108
|
+
def stop
|
109
|
+
@stop_timer = true
|
110
|
+
if(running?)
|
111
|
+
wakeup
|
112
|
+
clean_actions
|
113
|
+
@timer_thread.join
|
114
|
+
end
|
115
|
+
@timer_thread = nil
|
116
|
+
end
|
117
|
+
|
118
|
+
# owner:: owner actions to remove
|
119
|
+
# Clears timer of actions. If an owner is supplied
|
120
|
+
# only actions owned by owner will be removed
|
121
|
+
def clear(owner=nil)
|
122
|
+
if(owner.nil?)
|
123
|
+
@actions.clear
|
124
|
+
@new_actions.clear
|
125
|
+
else
|
126
|
+
@actions.each{|a| @actions.delete(a) if a.owner == owner}
|
127
|
+
end
|
128
|
+
wakeup
|
129
|
+
end
|
130
|
+
|
131
|
+
# Is timer currently running?
|
132
|
+
def running?
|
133
|
+
return !@timer_thread.nil?
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
def get_min_sleep
|
139
|
+
min = @actions.map{|a|a.remaining}.sort[0]
|
140
|
+
unless(min.nil? || min > 0)
|
141
|
+
@actions.each{|a|@actions.delete(a) if a.remaining == 0} # kill stuck actions
|
142
|
+
min = get_min_sleep
|
143
|
+
end
|
144
|
+
return min
|
145
|
+
end
|
146
|
+
|
147
|
+
def add_waiting_actions
|
148
|
+
@add_lock.synchronize do
|
149
|
+
@actions = @actions + @new_actions
|
150
|
+
@new_actions.clear
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def tick(time_passed)
|
155
|
+
@actions.each do |action|
|
156
|
+
action.tick(time_passed)
|
157
|
+
if(action.due?)
|
158
|
+
@actions.delete(action) if action.is_complete?
|
159
|
+
action = action.schedule
|
160
|
+
@pool.process do
|
161
|
+
begin
|
162
|
+
action.run
|
163
|
+
rescue StandardError => boom
|
164
|
+
@logger.error("Timer caught an error while running action: #{boom}\n#{boom.backtrace.join("\n")}")
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def clean_actions
|
172
|
+
@actions.clear
|
173
|
+
@new_actions.clear
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
end
|
data/test
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'actiontimer'
|
2
|
+
timer = ActionTimer::Timer.new
|
3
|
+
result = 0
|
4
|
+
actions = []
|
5
|
+
actions << ActionTimer::Action.new(@timer, 0.1){ result += 1}
|
6
|
+
actions << ActionTimer::Action.new(@timer, 0.2){ result += 1}
|
7
|
+
actions << ActionTimer::Action.new(@timer, 0.3){ result += 1}
|
8
|
+
timer.mass_add(actions)
|
9
|
+
sleep(0.41)
|
10
|
+
p result
|
11
|
+
|
data/tests/run_tests.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path("#{__FILE__}/../../lib"))
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'actiontimer'
|
5
|
+
|
6
|
+
class TimerTests < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@timer = ActionTimer::Timer.new
|
9
|
+
end
|
10
|
+
|
11
|
+
# Simple test of basic repetitive action
|
12
|
+
def test_basic
|
13
|
+
result = 0
|
14
|
+
@timer.add(2){ result += 1 }
|
15
|
+
sleep(5)
|
16
|
+
assert_equal(2, result)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Check the the running? method properly reports
|
20
|
+
def test_running
|
21
|
+
@timer.add(1){ 1 + 1}
|
22
|
+
assert(@timer.running?)
|
23
|
+
@timer.pause
|
24
|
+
assert(!@timer.running?)
|
25
|
+
@timer.start
|
26
|
+
assert(@timer.running?)
|
27
|
+
@timer.stop
|
28
|
+
assert(!@timer.running?)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Check that a value 0 < t < 1 works
|
32
|
+
# as expected
|
33
|
+
def test_float
|
34
|
+
result = 0
|
35
|
+
@timer.add(0.1){ result += 1 }
|
36
|
+
sleep(1.01)
|
37
|
+
assert_equal(10, result)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Check that a single iterative action is only
|
41
|
+
# completed once
|
42
|
+
def test_once
|
43
|
+
result = 0
|
44
|
+
@timer.add(1, true){ result += 1 }
|
45
|
+
sleep(3)
|
46
|
+
assert_equal(1, result)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Check that timer can be paused and restarted
|
50
|
+
# without registered actions being effected
|
51
|
+
def test_pause
|
52
|
+
result = 0
|
53
|
+
@timer.add(1){ result += 1 }
|
54
|
+
sleep(3.1)
|
55
|
+
@timer.pause
|
56
|
+
sleep(2)
|
57
|
+
@timer.start
|
58
|
+
sleep(2)
|
59
|
+
assert_equal(5, result)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Check that data can be passed to the block
|
63
|
+
# properly when created
|
64
|
+
def test_data
|
65
|
+
result = 0
|
66
|
+
@timer.add(1, true, 3){|a| result = a}
|
67
|
+
sleep(2)
|
68
|
+
assert_equal(3, result)
|
69
|
+
@timer.add(1, true, [3,4,['foobar']]){|a,b,c| result = [b,a,c]}
|
70
|
+
sleep(2)
|
71
|
+
assert_equal(4, result[0])
|
72
|
+
assert_equal(3, result[1])
|
73
|
+
assert(result[2].is_a?(Array))
|
74
|
+
end
|
75
|
+
|
76
|
+
# Check that the timer's auto starting mechanism
|
77
|
+
# can be disabled
|
78
|
+
def test_auto_start
|
79
|
+
timer = ActionTimer::Timer.new(:auto_start => false)
|
80
|
+
timer.add(1){ 1+1 }
|
81
|
+
assert(!timer.running?)
|
82
|
+
timer.start
|
83
|
+
assert(timer.running?)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Check that the actions can be cleared out of the
|
87
|
+
# timer and the timer is still left in a "running"
|
88
|
+
# state.
|
89
|
+
def test_clear
|
90
|
+
result = 0
|
91
|
+
@timer.add(1){ result += 1 }
|
92
|
+
sleep(3)
|
93
|
+
@timer.clear
|
94
|
+
sleep(2)
|
95
|
+
assert_equal(2, result)
|
96
|
+
assert(@timer.running?)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Check that the timer throws an exception when it
|
100
|
+
# is instructed to wakeup while not running
|
101
|
+
def test_wakeup
|
102
|
+
@timer.stop
|
103
|
+
assert_raise(ActionTimer::NotRunning){ @timer.wakeup }
|
104
|
+
end
|
105
|
+
|
106
|
+
# Check that the timer throws an exception when it
|
107
|
+
# is instructed to start but is already running
|
108
|
+
def test_start
|
109
|
+
assert_raise(ActionTimer::AlreadyRunning){ @timer.start }
|
110
|
+
end
|
111
|
+
|
112
|
+
# Check that multiple actions can be added at once
|
113
|
+
def test_mass_add
|
114
|
+
result = 0
|
115
|
+
actions = []
|
116
|
+
actions << ActionTimer::Action.new(@timer, 1){ result += 1}
|
117
|
+
actions << ActionTimer::Action.new(@timer, 3){ result += 1}
|
118
|
+
actions << ActionTimer::Action.new(@timer, 5){ result += 1}
|
119
|
+
@timer.mass_add(actions)
|
120
|
+
sleep(5.3)
|
121
|
+
assert_equal(7, result)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Check that an action can be properly removed from
|
125
|
+
# the timer
|
126
|
+
def test_remove
|
127
|
+
result = 0
|
128
|
+
action = @timer.add(1){result += 1}
|
129
|
+
sleep(2.1)
|
130
|
+
@timer.remove(action)
|
131
|
+
sleep(2)
|
132
|
+
assert_equal(2, result)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Check that an action's period can be dynamically
|
136
|
+
# reset
|
137
|
+
def test_action_reset
|
138
|
+
result = 0
|
139
|
+
action = @timer.add(1){ result += 1}
|
140
|
+
sleep(2.1)
|
141
|
+
action.reset_period(3)
|
142
|
+
sleep(3.1)
|
143
|
+
assert_equal(result, 3)
|
144
|
+
end
|
145
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: actiontimer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- spox
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ActionPool
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: ActionTimer is a simple timer for recurring actions. It supports single and recurring actions with an easy to use API.
|
26
|
+
email: spox@modspox.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- LICENSE
|
34
|
+
- CHANGELOG
|
35
|
+
files:
|
36
|
+
- lib
|
37
|
+
- lib/actiontimer
|
38
|
+
- lib/actiontimer/Exceptions.rb
|
39
|
+
- lib/actiontimer/Timer.rb
|
40
|
+
- lib/actiontimer/Action.rb
|
41
|
+
- lib/actiontimer.rb
|
42
|
+
- test
|
43
|
+
- tests
|
44
|
+
- tests/run_tests.rb
|
45
|
+
- CHANGELOG
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- actiontimer.gemspec
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/spox/actiontimer
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --title
|
54
|
+
- ActionTimer
|
55
|
+
- --main
|
56
|
+
- README.rdoc
|
57
|
+
- --line-numbers
|
58
|
+
- --inline-source
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.8.6
|
66
|
+
version:
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.3.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 2
|
79
|
+
summary: Simple timer for a complex world
|
80
|
+
test_files: []
|
81
|
+
|