robustthread 0.1 → 0.2
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/lib/robustthread.rb +49 -15
- metadata +2 -2
data/lib/robustthread.rb
CHANGED
@@ -5,30 +5,64 @@
|
|
5
5
|
# Copyright:: Copyright (c) 2009 Jared Kuolt
|
6
6
|
# License:: MIT License
|
7
7
|
|
8
|
-
|
8
|
+
class RobustThread
|
9
|
+
# The Thread object, brah
|
10
|
+
attr_reader :thread
|
11
|
+
# If the Thread takes a poopie...
|
12
|
+
attr_reader :exception
|
13
|
+
@@exit_handler_initialized = false
|
14
|
+
@@exception_handler = nil
|
9
15
|
# Usage:
|
10
16
|
#
|
11
|
-
# RobustThread.new(args) do |x, y|
|
17
|
+
# rt = RobustThread.new(args) do |x, y|
|
12
18
|
# do_something(x, y)
|
13
19
|
# end
|
14
20
|
#
|
15
|
-
|
16
|
-
|
17
|
-
|
21
|
+
# If necessary, you can access the actual thread from the +RobustThread+
|
22
|
+
# object via its +thread+ attribute.
|
23
|
+
#
|
24
|
+
# rt.thread
|
25
|
+
# => #<Thread:0x7fa1ea57ff88 run>
|
26
|
+
def initialize(*args, &block)
|
27
|
+
RobustThread.init_exit_handler
|
28
|
+
@thread = Thread.new(*args) do |*targs|
|
29
|
+
begin
|
30
|
+
block.call(*targs)
|
31
|
+
rescue => e
|
32
|
+
@exception = e
|
33
|
+
RobustThread.handle_exception(e)
|
34
|
+
end
|
18
35
|
end
|
19
|
-
thread[:real_ultimate_power] = true
|
20
|
-
thread
|
36
|
+
@thread[:real_ultimate_power] = true
|
21
37
|
end
|
22
38
|
|
23
|
-
|
24
|
-
|
39
|
+
# Set exception handler:
|
40
|
+
#
|
41
|
+
# RobustThread.exception_handler do |exception|
|
42
|
+
# handle_exception(exception)
|
43
|
+
# end
|
44
|
+
def RobustThread.exception_handler(&block)
|
45
|
+
unless block.arity == 1
|
46
|
+
raise ArgumentError, "Bad arity for exception handler. It may only accept a single argument"
|
47
|
+
end
|
48
|
+
@@exception_handler = block
|
49
|
+
end
|
25
50
|
|
26
|
-
|
27
|
-
unless
|
28
|
-
|
29
|
-
|
30
|
-
|
51
|
+
private
|
52
|
+
# Sets up the exit_handler unless @@exit_handler_initialized
|
53
|
+
def RobustThread.init_exit_handler
|
54
|
+
return if @@exit_handler_initialized
|
55
|
+
at_exit do
|
56
|
+
Thread.list.each do |thread|
|
57
|
+
thread.join if thread[:real_ultimate_power]
|
58
|
+
end
|
31
59
|
end
|
60
|
+
@@exit_handler_initialized = true
|
61
|
+
end
|
62
|
+
|
63
|
+
# Calls exception handler if set (see RobustThread.exception_handler)
|
64
|
+
def RobustThread.handle_exception(exception)
|
65
|
+
return unless @@exception_handler
|
66
|
+
@@exception_handler.call(exception)
|
32
67
|
end
|
33
|
-
RobustThread::BEGUN = true
|
34
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: robustthread
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared Kuolt
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-03 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|