JaredKuolt-robustthread 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 +68 -0
- metadata +53 -0
data/lib/robustthread.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# This module allows for the creation of a thread that will not simply die when
|
2
|
+
# the process dies. Instead, it joins all RobustThreads in Ruby's exit handler.
|
3
|
+
#
|
4
|
+
# Author:: Jared Kuolt (mailto:me@superjared.com)
|
5
|
+
# Copyright:: Copyright (c) 2009 Jared Kuolt
|
6
|
+
# License:: MIT License
|
7
|
+
|
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
|
15
|
+
# Usage:
|
16
|
+
#
|
17
|
+
# rt = RobustThread.new(args) do |x, y|
|
18
|
+
# do_something(x, y)
|
19
|
+
# end
|
20
|
+
#
|
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
|
35
|
+
end
|
36
|
+
@thread[:real_ultimate_power] = true
|
37
|
+
end
|
38
|
+
|
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
|
50
|
+
|
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
|
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)
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: JaredKuolt-robustthread
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jared Kuolt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Trivial module that allows you to create threads that are not killed if the process exits cleanly
|
17
|
+
email: me@superjared.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/robustthread.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://github.com/JaredKuolt/robustthread/tree/master
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: "0"
|
38
|
+
version:
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
rubyforge_project: robustthread
|
48
|
+
rubygems_version: 1.2.0
|
49
|
+
signing_key:
|
50
|
+
specification_version: 2
|
51
|
+
summary: Threads that stay alive
|
52
|
+
test_files: []
|
53
|
+
|