JaredKuolt-robustthread 0.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/robustthread.rb +16 -1
- metadata +1 -1
data/lib/robustthread.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
# Copyright:: Copyright (c) 2009 Jared Kuolt
|
3
3
|
# License:: MIT License
|
4
4
|
require 'logger'
|
5
|
+
require 'timeout'
|
5
6
|
|
6
7
|
class RobustThread
|
7
8
|
# The Thread object, brah
|
@@ -54,7 +55,21 @@ class RobustThread
|
|
54
55
|
Kernel.loop do
|
55
56
|
break if self.say_goodnight
|
56
57
|
block.call(*args)
|
57
|
-
sleep
|
58
|
+
# We want to sleep for the right amount of time, but we also don't
|
59
|
+
# want to wait until the sleep is done if our exit handler has been
|
60
|
+
# called so we iterate over a loop, sleeping only 0.1 and checking
|
61
|
+
# each iteration whether we need to die, and the timeout is a noop
|
62
|
+
# indicating we need to continue.
|
63
|
+
begin
|
64
|
+
Timeout.timeout(sleep_seconds) do
|
65
|
+
Kernel.loop do
|
66
|
+
break if self.say_goodnight
|
67
|
+
sleep 0.1
|
68
|
+
end
|
69
|
+
end
|
70
|
+
rescue Timeout::Error
|
71
|
+
# OK
|
72
|
+
end
|
58
73
|
end
|
59
74
|
end
|
60
75
|
end
|