ghazel-SystemTimer 1.2.1.1-x86-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- 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 +62 -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,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ghazel-SystemTimer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1.1
|
5
|
+
platform: x86-mswin32
|
6
|
+
authors:
|
7
|
+
- Philippe Hanrigou
|
8
|
+
- David Vollbracht
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-12-09 00:00:00 -08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email:
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- lib/system_timer.rb
|
27
|
+
- lib/system_timer_stub.rb
|
28
|
+
- README
|
29
|
+
has_rdoc: true
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --title
|
36
|
+
- SystemTimer
|
37
|
+
- --main
|
38
|
+
- README
|
39
|
+
- --line-numbers
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: systemtimer
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Set a Timeout based on signals, which are more reliable than Timeout. Timeout is based on green threads.
|
61
|
+
test_files:
|
62
|
+
- test/all_tests.rb
|