robustthread 0.4.1 → 0.5
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 +33 -1
- metadata +2 -2
data/lib/robustthread.rb
CHANGED
@@ -16,9 +16,12 @@ class RobustThread
|
|
16
16
|
def initialize(opts={}, &block)
|
17
17
|
self.class.send :init_exit_handler
|
18
18
|
args = opts[:args] or []
|
19
|
+
self.class.send :do_before_init
|
19
20
|
@thread = Thread.new(*args) do |*targs|
|
20
21
|
begin
|
22
|
+
self.class.send :do_before_yield
|
21
23
|
block.call(*targs)
|
24
|
+
self.class.send :do_after_yield
|
22
25
|
rescue => e
|
23
26
|
@exception = e
|
24
27
|
self.class.send :handle_exception, e
|
@@ -31,7 +34,8 @@ class RobustThread
|
|
31
34
|
|
32
35
|
## Class methods and attributes
|
33
36
|
class << self
|
34
|
-
attr_accessor :logger, :say_goodnight, :exit_handler_initialized
|
37
|
+
attr_accessor :logger, :say_goodnight, :exit_handler_initialized, :callbacks
|
38
|
+
VALID_CALLBACKS = [:before_init, :before_yield, :after_yield, :after_join]
|
35
39
|
|
36
40
|
# Logger object (see README)
|
37
41
|
def logger
|
@@ -82,6 +86,16 @@ class RobustThread
|
|
82
86
|
@exception_handler = block
|
83
87
|
end
|
84
88
|
|
89
|
+
# Add a callback
|
90
|
+
public
|
91
|
+
def add_callback(sym, &block)
|
92
|
+
sym = sym.to_sym
|
93
|
+
raise ArgumentError, "Invalid callback #{sym.inspect}" unless VALID_CALLBACKS.include? sym
|
94
|
+
self.callbacks ||= {}
|
95
|
+
self.callbacks[sym] ||= []
|
96
|
+
self.callbacks[sym] << block
|
97
|
+
end
|
98
|
+
|
85
99
|
private
|
86
100
|
# Calls exception handler if set (see RobustThread.exception_handler)
|
87
101
|
def handle_exception(exc)
|
@@ -103,6 +117,7 @@ class RobustThread
|
|
103
117
|
self.group.each do |rt|
|
104
118
|
log "waiting on #{rt.label.inspect}"
|
105
119
|
rt.thread.join
|
120
|
+
rt.class.send :do_after_join
|
106
121
|
end
|
107
122
|
log "exited cleanly"
|
108
123
|
rescue Interrupt
|
@@ -111,5 +126,22 @@ class RobustThread
|
|
111
126
|
end
|
112
127
|
self.exit_handler_initialized = true
|
113
128
|
end
|
129
|
+
|
130
|
+
def perform_callback(sym)
|
131
|
+
raise ArgumentError, "Cannot perform invalid callback #{sym.inspect}" unless VALID_CALLBACKS.include? sym
|
132
|
+
return unless self.callbacks and self.callbacks[sym]
|
133
|
+
self.callbacks[sym].reverse.each do |callback|
|
134
|
+
callback.call
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Performs callback, if possible
|
139
|
+
def method_missing(sym, *args)
|
140
|
+
if sym.to_s =~ /^do_(.*)$/
|
141
|
+
perform_callback($1.to_sym)
|
142
|
+
else
|
143
|
+
raise NoMethodError, "RobustThread method_missing: #{sym.inspect}"
|
144
|
+
end
|
145
|
+
end
|
114
146
|
end
|
115
147
|
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.5"
|
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-18 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|