ghazel-SystemTimer 1.2.1.1-i386-mingw32
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/README +1 -0
- data/lib/system_timer.rb +115 -0
- data/lib/system_timer_stub.rb +20 -0
- data/test/all_tests.rb +3 -0
- metadata +76 -0
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
README.markdown
|
data/lib/system_timer.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# Copyright 2008 David Vollbracht & Philippe Hanrigou
|
2
|
+
|
3
|
+
if RUBY_PLATFORM =~ /mswin|mingw/ or (defined?(RUBY_ENGINE) and RUBY_ENGINE == "rbx")
|
4
|
+
require File.dirname(__FILE__) + '/system_timer_stub'
|
5
|
+
else
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'timeout'
|
9
|
+
require 'forwardable'
|
10
|
+
require 'monitor'
|
11
|
+
require File.dirname(__FILE__) + '/system_timer/thread_timer'
|
12
|
+
require File.dirname(__FILE__) + '/system_timer/concurrent_timer_pool'
|
13
|
+
|
14
|
+
# Timer based on underlying +ITIMER_REAL+ system timer. It is a
|
15
|
+
# solution to Ruby processes which hang beyond the time limit when accessing
|
16
|
+
# external resources. This is useful when timeout.rb, which relies on green
|
17
|
+
# threads, does not work consistently.
|
18
|
+
#
|
19
|
+
# For more information and background check out:
|
20
|
+
#
|
21
|
+
# * http://ph7spot.com/articles/system_timer
|
22
|
+
# * http://davidvollbracht.com/2008/6/2/30-days-of-teach-day-1-systemtimer
|
23
|
+
#
|
24
|
+
# == Usage
|
25
|
+
#
|
26
|
+
# require 'systemtimer'
|
27
|
+
#
|
28
|
+
# SystemTimer.timeout_after(5) do
|
29
|
+
#
|
30
|
+
# # Something that should be interrupted if it takes too much time...
|
31
|
+
# # ... even if blocked on a system call!
|
32
|
+
#
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
module SystemTimer
|
36
|
+
|
37
|
+
Thread.exclusive do # Avoid race conditions for monitor and pool creation
|
38
|
+
@timer_pool = ConcurrentTimerPool.new
|
39
|
+
@monitor = Monitor.new
|
40
|
+
end
|
41
|
+
|
42
|
+
class << self
|
43
|
+
attr_reader :timer_pool
|
44
|
+
|
45
|
+
# Executes the method's block. If the block execution terminates before
|
46
|
+
# +seconds+ seconds has passed, it returns true. If not, it terminates
|
47
|
+
# the execution and raises a +Timeout::Error+.
|
48
|
+
def timeout_after(seconds, exception_class = nil)
|
49
|
+
new_timer = nil # just for scope
|
50
|
+
@monitor.synchronize do
|
51
|
+
new_timer = timer_pool.add_timer seconds, exception_class
|
52
|
+
timer_interval = timer_pool.next_trigger_interval_in_seconds
|
53
|
+
debug "==== Install Timer ==== at #{Time.now.to_f}, next interval: #{timer_interval}"
|
54
|
+
if timer_pool.first_timer?
|
55
|
+
install_first_timer_and_save_original_configuration timer_interval
|
56
|
+
else
|
57
|
+
install_next_timer timer_interval
|
58
|
+
end
|
59
|
+
end
|
60
|
+
return yield
|
61
|
+
ensure
|
62
|
+
@monitor.synchronize do
|
63
|
+
debug "==== Cleanup Timer ==== at #{Time.now.to_f}, #{new_timer} "
|
64
|
+
timer_pool.cancel new_timer
|
65
|
+
timer_pool.log_registered_timers if debug_enabled?
|
66
|
+
next_interval = timer_pool.next_trigger_interval_in_seconds
|
67
|
+
debug "Cleanup Timer : next interval #{next_interval.inspect} "
|
68
|
+
if next_interval
|
69
|
+
install_next_timer next_interval
|
70
|
+
else
|
71
|
+
restore_original_configuration
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Backward compatibility with timeout.rb
|
77
|
+
alias timeout timeout_after
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def install_ruby_sigalrm_handler #:nodoc:
|
82
|
+
@original_ruby_sigalrm_handler = trap('SIGALRM') do
|
83
|
+
@monitor.synchronize do
|
84
|
+
# Triggers timers one at a time to ensure more deterministic results
|
85
|
+
timer_pool.trigger_next_expired_timer
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def restore_original_ruby_sigalrm_handler #:nodoc:
|
91
|
+
trap('SIGALRM', original_ruby_sigalrm_handler || 'DEFAULT')
|
92
|
+
ensure
|
93
|
+
reset_original_ruby_sigalrm_handler
|
94
|
+
end
|
95
|
+
|
96
|
+
def original_ruby_sigalrm_handler #:nodoc:
|
97
|
+
@original_ruby_sigalrm_handler
|
98
|
+
end
|
99
|
+
|
100
|
+
def reset_original_ruby_sigalrm_handler #:nodoc:
|
101
|
+
@original_ruby_sigalrm_handler = nil
|
102
|
+
end
|
103
|
+
|
104
|
+
def debug(message) #:nodoc
|
105
|
+
puts message if debug_enabled?
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
require 'system_timer_native'
|
113
|
+
|
114
|
+
|
115
|
+
end # stub guard
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Copyright 2008 David Vollbracht & Philippe Hanrigou
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'timeout'
|
5
|
+
|
6
|
+
module SystemTimer
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def timeout_after(seconds)
|
10
|
+
Timeout::timeout(seconds) do
|
11
|
+
yield
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Backward compatibility with timeout.rb
|
16
|
+
alias timeout timeout_after
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/test/all_tests.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ghazel-SystemTimer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 73
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
- 1
|
11
|
+
version: 1.2.1.1
|
12
|
+
platform: i386-mingw32
|
13
|
+
authors:
|
14
|
+
- Philippe Hanrigou
|
15
|
+
- David Vollbracht
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-12-10 00:00:00 -08:00
|
21
|
+
default_executable:
|
22
|
+
dependencies: []
|
23
|
+
|
24
|
+
description:
|
25
|
+
email:
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- lib/system_timer.rb
|
34
|
+
- lib/system_timer_stub.rb
|
35
|
+
- test/all_tests.rb
|
36
|
+
- README
|
37
|
+
has_rdoc: true
|
38
|
+
homepage:
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --title
|
44
|
+
- SystemTimer
|
45
|
+
- --main
|
46
|
+
- README
|
47
|
+
- --line-numbers
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: systemtimer
|
71
|
+
rubygems_version: 1.3.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Set a Timeout based on signals, which are more reliable than Timeout. Timeout is based on green threads.
|
75
|
+
test_files:
|
76
|
+
- test/all_tests.rb
|