robustthread 0.5 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +74 -0
  2. data/lib/robustthread.rb +6 -2
  3. metadata +5 -4
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ Author:: Jared Kuolt (mailto:me@superjared.com)
2
+ Copyright:: Copyright (c) 2009 Jared Kuolt
3
+ License:: MIT License
4
+
5
+ = RobustThread
6
+
7
+ This module allows for the creation of a thread that will not simply die when
8
+ the process dies. Instead, it joins all RobustThreads in Ruby's exit handler.
9
+
10
+ == Installation
11
+
12
+ sudo gem install robustthread
13
+
14
+ == Usage
15
+
16
+ rt = RobustThread.new(:args => args, :label => "do_something with x and y") do |x, y|
17
+ do_something(x, y)
18
+ end
19
+
20
+ Options:
21
+ * +args+: arguments passed to the thread
22
+ * +label+: an identifier (used primarily in logs for debugging, defaults to
23
+ +thread.inspect+)
24
+
25
+ === Easy Looping
26
+
27
+ You can loop a task in a thread to cleanly exit:
28
+
29
+ RobustThread.loop(:seconds => 3) do
30
+ do_something
31
+ end
32
+
33
+ Options are the same as RobustThread#new, with the addition of +seconds+, the
34
+ interval at which the Thread will sleep.
35
+
36
+ === Exception Handling
37
+
38
+ Since Threads usually eat exceptions, RobustThread allows for a simple global
39
+ exception handler:
40
+
41
+ RobustThread.exception_handler do |exception|
42
+ # Handle your exceptions here
43
+ end
44
+
45
+ If no handler is assigned, the exception traceback will be piped into the
46
+ logger as an error message.
47
+
48
+ === Callbacks
49
+
50
+ RobustThread currently supports 5 callbacks. The following 4 are called per
51
+ RobustThread.
52
+
53
+ RobustThread.add_callback(:before_init){ puts "Before init!" }
54
+ RobustThread.add_callback(:before_yield){ puts "Before yield!" }
55
+ RobustThread.add_callback(:after_yield){ puts "After yield!" }
56
+ RobustThread.add_callback(:after_join){ puts "After join!" }
57
+
58
+ The +before_exit+ callback is called after all threads are re-joined.
59
+
60
+ RobustThread.add_callback(:before_exit){ puts "Before exit!" }
61
+
62
+ === Etc...
63
+
64
+ If necessary, you can access the actual thread from the RobustThread
65
+ object via its +thread+ attribute.
66
+
67
+ rt.thread
68
+ => #<Thread:0x7fa1ea57ff88 run>
69
+
70
+ By default, RobustThread uses a Logger that defaults itself to STDOUT. You
71
+ can change this by assigning the +logger+ class attribute to a different
72
+ Logger object:
73
+
74
+ RobustThread.logger = Logger.new(STDERR)
data/lib/robustthread.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # Author:: Jared Kuolt (mailto:me@superjared.com)
2
2
  # Copyright:: Copyright (c) 2009 Jared Kuolt
3
3
  # License:: MIT License
4
+ #
5
+ # See README.rdoc[link:files/README_rdoc.html] for usage
6
+ #
4
7
  require 'logger'
5
8
  require 'timeout'
6
9
 
@@ -35,7 +38,7 @@ class RobustThread
35
38
  ## Class methods and attributes
36
39
  class << self
37
40
  attr_accessor :logger, :say_goodnight, :exit_handler_initialized, :callbacks
38
- VALID_CALLBACKS = [:before_init, :before_yield, :after_yield, :after_join]
41
+ VALID_CALLBACKS = [:before_init, :before_yield, :after_yield, :after_join, :before_exit]
39
42
 
40
43
  # Logger object (see README)
41
44
  def logger
@@ -115,10 +118,11 @@ class RobustThread
115
118
  self.say_goodnight = true
116
119
  begin
117
120
  self.group.each do |rt|
118
- log "waiting on #{rt.label.inspect}"
121
+ log "waiting on #{rt.label.inspect}" if rt.thread.alive?
119
122
  rt.thread.join
120
123
  rt.class.send :do_after_join
121
124
  end
125
+ self.send :do_before_exit
122
126
  log "exited cleanly"
123
127
  rescue Interrupt
124
128
  log "prematurely killed by interrupt!", :error
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.5"
4
+ version: 0.5.1
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-18 00:00:00 -07:00
12
+ date: 2009-06-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -19,10 +19,11 @@ executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
- extra_rdoc_files: []
23
-
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
24
  files:
25
25
  - lib/robustthread.rb
26
+ - README.rdoc
26
27
  has_rdoc: true
27
28
  homepage: http://github.com/JaredKuolt/robustthread/tree/master
28
29
  post_install_message: