bkoski-interval_exec 0.5.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.
- data/History.txt +4 -0
- data/Manifest.txt +12 -0
- data/PostInstall.txt +1 -0
- data/README.rdoc +48 -0
- data/Rakefile +28 -0
- data/lib/interval_exec/interval_exec.rb +50 -0
- data/lib/interval_exec.rb +8 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_helper.rb +7 -0
- data/test/test_interval_exec.rb +105 -0
- metadata +87 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/PostInstall.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
You've successfully installed interval_exec!
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= interval_exec
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
Library to execute code at defined intervals -- useful in command-line scripts or daemons when a resource must be polled periodically.
|
6
|
+
|
7
|
+
== SYNOPSIS:
|
8
|
+
|
9
|
+
IntervalExec.run wraps a block of code to be executed at a given interval. For example,
|
10
|
+
|
11
|
+
IntervalExec.run(10, :immediate) do
|
12
|
+
puts "Hello, world!"
|
13
|
+
end
|
14
|
+
|
15
|
+
will put "Hello, world!" to STDOUT every 10 seconds. See IntervalExec docs for more detail; options provide a post-interval callback hook and change handling for blocks that run longer than the interval
|
16
|
+
|
17
|
+
== Author:
|
18
|
+
|
19
|
+
Ben Koski, http://benkoski.com
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
sudo gem install bkoski-interval_exec --source http://gems.github.com
|
24
|
+
|
25
|
+
== LICENSE:
|
26
|
+
|
27
|
+
(The MIT License)
|
28
|
+
|
29
|
+
Copyright (c) 2009 Ben Koski
|
30
|
+
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
a copy of this software and associated documentation files (the
|
33
|
+
'Software'), to deal in the Software without restriction, including
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be
|
40
|
+
included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
45
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
46
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
47
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
48
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/interval_exec'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('interval_exec', IntervalExecGem::VERSION) do |p|
|
7
|
+
p.developer('Ben Koski', 'ben@benkoski.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
# p.extra_deps = [
|
12
|
+
# ['activesupport','>= 2.0.2'],
|
13
|
+
# ]
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
16
|
+
]
|
17
|
+
|
18
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
19
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
20
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
21
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
25
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
26
|
+
|
27
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
28
|
+
# task :default => [:spec, :features]
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class IntervalExec
|
2
|
+
|
3
|
+
# Loops a block of code, handling execution interval as specified by _duration_ and _params_.
|
4
|
+
#
|
5
|
+
# *Params*:
|
6
|
+
# [+duration+] Execution interval in seconds
|
7
|
+
# [+overrun_handler+] Defines what should happen when the block execution exceeds duration
|
8
|
+
#
|
9
|
+
# <b>Possible overrun_handler values:</b>
|
10
|
+
# [<tt>:immediate</tt>] If execution exceeds duration, next interval begins immediately after long cycle completes.
|
11
|
+
# [<tt>:fixed</tt>] Next interval begins exactly _duration_ seconds after execution completes.
|
12
|
+
# [<tt>:next</tt>] Execution skips a cycle, and picks up at next scheduled interval. For example, if cycle is 10s and code
|
13
|
+
# execution takes 15s, IntervalExec will sleep for 5s so execution begins at 20s mark.
|
14
|
+
#
|
15
|
+
# *Options*:
|
16
|
+
# [<tt>:interval_end_proc</tt>] A proc to be called at the end of every interval with one parameter:
|
17
|
+
# +actual_duration+, block execution time in seconds
|
18
|
+
#
|
19
|
+
# <b>Notes:</b>
|
20
|
+
# * If code execution ends early, code sleeps until _duration_ is reached.
|
21
|
+
# * Code runs inside a standard +loop+, so +break+ and +next+ keywords can be used to stop execution and skip to next interval
|
22
|
+
# * IntervalExec doesn't do any exception handling
|
23
|
+
def self.run(duration, overrun_handler, opts = {}, &block)
|
24
|
+
interval_end_proc = opts[:interval_end_proc]
|
25
|
+
|
26
|
+
loop do
|
27
|
+
start_time = Time.now
|
28
|
+
|
29
|
+
yield
|
30
|
+
|
31
|
+
end_time = Time.now
|
32
|
+
actual_duration = end_time - start_time
|
33
|
+
|
34
|
+
if actual_duration < duration
|
35
|
+
Kernel.sleep(duration - actual_duration)
|
36
|
+
else
|
37
|
+
case overrun_handler
|
38
|
+
when :fixed
|
39
|
+
Kernel.sleep(duration)
|
40
|
+
when :next
|
41
|
+
overrun = actual_duration % duration
|
42
|
+
Kernel.sleep(duration - overrun)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
interval_end_proc.call(actual_duration) unless interval_end_proc.nil?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/interval_exec.rb'}"
|
9
|
+
puts "Loading interval_exec gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
# cattr_accessor to store expected sleep value
|
5
|
+
@@expected_sleep = nil
|
6
|
+
def self.expected_sleep= val
|
7
|
+
@@expected_sleep = val
|
8
|
+
end
|
9
|
+
|
10
|
+
def expected_sleep
|
11
|
+
@@expected_sleep
|
12
|
+
end
|
13
|
+
|
14
|
+
# Stubbed sleep raises exception if request isn't approx equal to expected_sleep
|
15
|
+
def self.sleep requested_sec
|
16
|
+
raise "No sleep expectation defined" if self.expected_sleep.nil?
|
17
|
+
sleep_delta = self.expected_sleep - requested_sec
|
18
|
+
|
19
|
+
tolerance = 1
|
20
|
+
if sleep_delta > tolerance || sleep_delta < (-1 * tolerance)
|
21
|
+
raise "Sleep request (#{requested_sec}) was #{sleep_delta} different from expectation (#{self.expected_sleep})!"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
class TestIntervalExec < Test::Unit::TestCase
|
28
|
+
|
29
|
+
def teardown
|
30
|
+
Time.unmock!
|
31
|
+
end
|
32
|
+
|
33
|
+
def run_interval duration, overflow_handler, block_execution_time, opts={}
|
34
|
+
first_cycle = true
|
35
|
+
IntervalExec.run(duration, overflow_handler, opts) do
|
36
|
+
break if !first_cycle
|
37
|
+
|
38
|
+
first_cycle = false
|
39
|
+
Time.freeze_time(Time.now + block_execution_time)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "code completes within interval" do
|
44
|
+
|
45
|
+
should "call sleep when overrun_handler=:immediate" do
|
46
|
+
Kernel.expects(:sleep)
|
47
|
+
run_interval(10, :immediate, 6)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "sleep until interval expires when overrun_handler=:immediate" do
|
51
|
+
Kernel.expected_sleep = 4
|
52
|
+
assert_nothing_raised { run_interval(10, :immediate, 6) }
|
53
|
+
end
|
54
|
+
|
55
|
+
should "sleep until interval expires when overrun_handler=:fixed" do
|
56
|
+
Kernel.expected_sleep = 6
|
57
|
+
assert_nothing_raised { run_interval(12, :fixed, 6) }
|
58
|
+
end
|
59
|
+
|
60
|
+
should "sleep until interval expires when overrun_handler=:next" do
|
61
|
+
Kernel.expected_sleep = 8
|
62
|
+
assert_nothing_raised { run_interval(14, :fixed, 6) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "code runs past interval" do
|
67
|
+
should "not sleep at all when overrun_handler=:immediate" do
|
68
|
+
Kernel.expects(:sleep).never
|
69
|
+
run_interval(14, :immediate, 20)
|
70
|
+
end
|
71
|
+
|
72
|
+
should "sleep when overrun_handler=:fixed" do
|
73
|
+
Kernel.expects(:sleep)
|
74
|
+
assert_nothing_raised { run_interval(14, :fixed, 20) }
|
75
|
+
end
|
76
|
+
|
77
|
+
should "sleep for duration when overrun_handler=:fixed" do
|
78
|
+
Kernel.expected_sleep = 14
|
79
|
+
assert_nothing_raised { run_interval(14, :fixed, 20) }
|
80
|
+
end
|
81
|
+
|
82
|
+
should "sleep when overrun_handler=:next" do
|
83
|
+
Kernel.expects(:sleep)
|
84
|
+
assert_nothing_raised { run_interval(15, :next, 20) }
|
85
|
+
end
|
86
|
+
|
87
|
+
should "sleep until interval would expire when overrun_handler=:next" do
|
88
|
+
Kernel.expected_sleep = 10
|
89
|
+
assert_nothing_raised { run_interval(15, :next, 20) }
|
90
|
+
end
|
91
|
+
|
92
|
+
should "sleep until interval would expire when overrun_handler=:next and execution exceeds several intervals" do
|
93
|
+
Kernel.expected_sleep = 10
|
94
|
+
assert_nothing_raised { run_interval(15, :next, 35) }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
should "call interval_end_proc when specified" do
|
99
|
+
Kernel.stubs(:sleep)
|
100
|
+
test_proc = Proc.new {}
|
101
|
+
test_proc.expects(:call)
|
102
|
+
run_interval(10, :immediate, 5, :interval_end_proc => test_proc)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bkoski-interval_exec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Koski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-12 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.2.3
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: hoe
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.8.0
|
32
|
+
version:
|
33
|
+
description: Library to execute code at defined intervals -- useful in command-line scripts or daemons when a resource must be polled periodically.
|
34
|
+
email:
|
35
|
+
- ben@benkoski.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- History.txt
|
42
|
+
- Manifest.txt
|
43
|
+
- PostInstall.txt
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- History.txt
|
47
|
+
- Manifest.txt
|
48
|
+
- PostInstall.txt
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- lib/interval_exec/interval_exec.rb
|
52
|
+
- lib/interval_exec.rb
|
53
|
+
- script/console
|
54
|
+
- script/destroy
|
55
|
+
- script/generate
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/test_interval_exec.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage:
|
60
|
+
post_install_message: PostInstall.txt
|
61
|
+
rdoc_options:
|
62
|
+
- --main
|
63
|
+
- README.rdoc
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: interval_exec
|
81
|
+
rubygems_version: 1.2.0
|
82
|
+
signing_key:
|
83
|
+
specification_version: 2
|
84
|
+
summary: Library to execute code at defined intervals -- useful in command-line scripts or daemons when a resource must be polled periodically.
|
85
|
+
test_files:
|
86
|
+
- test/test_helper.rb
|
87
|
+
- test/test_interval_exec.rb
|