ActionPool 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ActionPool-0.0.1.gem CHANGED
Binary file
File without changes
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.rdoc ADDED
@@ -0,0 +1,175 @@
1
+ == ActionPool
2
+
3
+ ActionPool is just a simple thread pool. It allows for various contraints and resizing in a pretty easy and unobtrusive manner. You can set limits on how long tasks are worked on, as well as on the life of a thread. For things that like to use lots threads, it can be helpful to reuse threads instead of constantly recreating them.
4
+
5
+ === install (easy):
6
+
7
+ gem install ActionPool
8
+
9
+ === install (less easy):
10
+
11
+ git clone http://github.com/spox/actionpool.git
12
+ cd actionpool
13
+ gem build *.gemspec
14
+ gem install ./
15
+
16
+ === install (less easy that's a little easier)
17
+
18
+ {rip}[http://hellorip.com/about.html] makes it easy to install directly from a github repository.
19
+
20
+ == Documentation
21
+
22
+ {rdocs}[http://allgems.ruby-forum.com/gems/ActionPool/]
23
+
24
+ == Example
25
+
26
+ === Code:
27
+
28
+ require 'actionpool'
29
+
30
+ pool = ActionPool::Pool.new
31
+ pool.process do
32
+ sleep(2)
33
+ raise 'Wakeup main thread'
34
+ end
35
+ 20.times do
36
+ pool.process do
37
+ puts "Thread: #{Thread.current}"
38
+ sleep(0.1)
39
+ end
40
+ end
41
+ begin
42
+ sleep
43
+ rescue Exception => e
44
+ puts "Thread pool woke me up: #{e}"
45
+ end
46
+
47
+
48
+ === Result:
49
+
50
+ Thread: #<Thread:0x93ebeb8>
51
+ Thread: #<Thread:0x93eb92c>
52
+ Thread: #<Thread:0x93eb8a0>
53
+ Thread: #<Thread:0x93eb814>
54
+ Thread: #<Thread:0x93eb788>
55
+ Thread: #<Thread:0x93eb670>
56
+ Thread: #<Thread:0x93eb5e4>
57
+ Thread: #<Thread:0x93eb558>
58
+ Thread: #<Thread:0x93eb4cc>
59
+ Thread: #<Thread:0x93ebeb8>
60
+ Thread: #<Thread:0x93eb92c>
61
+ Thread: #<Thread:0x93eb8a0>
62
+ Thread: #<Thread:0x93eb814>
63
+ Thread: #<Thread:0x93eb788>
64
+ Thread: #<Thread:0x93eb670>
65
+ Thread: #<Thread:0x93eb5e4>
66
+ Thread: #<Thread:0x93eb558>
67
+ Thread: #<Thread:0x93eb4cc>
68
+ Thread: #<Thread:0x93eb92c>
69
+ Thread: #<Thread:0x93eb8a0>
70
+ Thread pool woke me up: Wakeup main thread
71
+
72
+ === Important note
73
+
74
+ The worker threads in the ActionPool will catch all Exception objects that your block fails to catch. Instead of just eating these exceptions, they are passed back to the creating thread of the pool (generally the main thread). This is important to note if you care about processing unexpected exceptions and what allows the example code above to be woken up from its sleep.
75
+
76
+ == The internals
77
+ ==== Probably more information than you really want
78
+
79
+ ActionPool has some simple settings that make things work. First, the pool has a minimum and maximum number of allowed threads. On initialization, the minimum number of threads are created and put into the pool. By default, this is 10 threads. As the number of tasks added to the pool increases, the pool will grow as needed. When more tasks are in the pool than threads to process them, new threads will be added into the pool, until the maximum thread threshold is reached. Taking the example above, we can demonstrate this easily by adjusting our limits:
80
+
81
+ require 'actionpool'
82
+
83
+ pool = ActionPool::Pool.new(:min_threads => 1, :max_threads => 3)
84
+ pool.process do
85
+ sleep(10)
86
+ raise 'Wakeup main thread'
87
+ end
88
+ 20.times do
89
+ pool.process do
90
+ puts "Thread: #{Thread.current}"
91
+ sleep(rand(0.0))
92
+ end
93
+ end
94
+ begin
95
+ sleep
96
+ rescue Exception => e
97
+ puts "Thread pool woke me up: #{e}"
98
+ end
99
+
100
+ Which results in:
101
+
102
+ Thread: #<Thread:0x86c1760>
103
+ Thread: #<Thread:0x86c1080>
104
+ Thread: #<Thread:0x86c1080>
105
+ Thread: #<Thread:0x86c1080>
106
+ Thread: #<Thread:0x86c1760>
107
+ Thread: #<Thread:0x86c1760>
108
+ Thread: #<Thread:0x86c1760>
109
+ Thread: #<Thread:0x86c1080>
110
+ Thread: #<Thread:0x86c1760>
111
+ Thread: #<Thread:0x86c1080>
112
+ Thread: #<Thread:0x86c1080>
113
+ Thread: #<Thread:0x86c1760>
114
+ Thread: #<Thread:0x86c1080>
115
+ Thread: #<Thread:0x86c1760>
116
+ Thread: #<Thread:0x86c1760>
117
+ Thread: #<Thread:0x86c1760>
118
+ Thread: #<Thread:0x86c1080>
119
+ Thread: #<Thread:0x86c1760>
120
+ Thread: #<Thread:0x86c1760>
121
+ Thread: #<Thread:0x86c1080>
122
+ Thread pool woke me up: Wakeup main thread
123
+
124
+ Our pool starts with a single thread that is occupied by the sleeping task waiting to raise an exception. As we begin to add new tasks, the pool grows to accommidate the growing number of tasks, until it reaches the maximum threshold of 3. At that point, the pool simply processes the tasks until the task list is empty.
125
+
126
+ The pool also has the ability to limit the amount of time a thread spends working on a given task. By default, a thread will work on a given task until the task is completed, or the pool is shutdown. However, as the following example shows, it is very easy to limit this time to avoid the pool being bogged down on long running tasks:
127
+
128
+ require 'actionpool'
129
+
130
+ pool = ActionPool::Pool.new(:min_threads => 1, :max_threads => 1, :a_to => 1)
131
+ pool.process do
132
+ puts "#{Time.now}: I'm a long running task"
133
+ sleep(100)
134
+ raise 'Wakeup main thread'
135
+ end
136
+ pool.process do
137
+ puts "#{Time.now}: Waiting for my turn"
138
+ raise "I'm waking up the main thread"
139
+ end
140
+ begin
141
+ sleep
142
+ rescue Exception => e
143
+ puts "Thread pool woke me up: #{e}"
144
+ end
145
+
146
+ Results:
147
+
148
+ 2009-10-10 08:47:08 -0700: I'm a long running task
149
+ 2009-10-10 08:47:09 -0700: Waiting for my turn
150
+ Thread pool woke me up: I'm waking up the main thread
151
+
152
+ If you have a number of tasks you would like to schedule at once, it is easy with the add_jobs method:
153
+
154
+ require 'actionpool'
155
+
156
+ pool = ActionPool::Pool.new
157
+ a = 0
158
+ lock = Mutex.new
159
+ tasks = [].fill(lambda{ lock.synchronize{ a += 1 } }, 0..19)
160
+ pool.add_jobs(tasks)
161
+ sleep(0.5)
162
+ puts "Result: #{a}"
163
+
164
+ Results:
165
+
166
+ Result: 20
167
+
168
+ === Footer
169
+
170
+ I hope this library is useful. If you find any bugs, or need any help, you can find me on DALnet and Freenode.
171
+
172
+ == License
173
+
174
+ ActionPool is licensed under the LGPLv3
175
+ Copyright (c) 2009 spox <spox@modspox.com>
data/actionpool.gemspec CHANGED
@@ -2,11 +2,22 @@ spec = Gem::Specification.new do |s|
2
2
  s.name = 'ActionPool'
3
3
  s.author = %q(spox)
4
4
  s.email = %q(spox@modspox.com)
5
- s.version = '0.0.1'
5
+ s.version = '0.1.0'
6
6
  s.summary = %q(Thread Pool)
7
7
  s.platform = Gem::Platform::RUBY
8
- s.has_rdoc = true
9
8
  s.files = Dir['**/*']
9
+ s.rdoc_options = %w(--title ActionPool --main README.rdoc --line-numbers)
10
+ s.extra_rdoc_files = %w(README.rdoc)
10
11
  s.require_paths = %w(lib)
11
12
  s.required_ruby_version = '>= 1.8.6'
13
+ s.homepage = %q(http://github.com/spox/actionpool)
14
+ description = []
15
+ File.open("README.rdoc") do |file|
16
+ file.each do |line|
17
+ line.chomp!
18
+ break if line.empty?
19
+ description << "#{line.gsub(/\[\d\]/, '')}"
20
+ end
21
+ end
22
+ s.description = description[1..-1].join(" ")
12
23
  end
@@ -1,35 +1,37 @@
1
- require 'actionpool/Exceptions'
2
1
  require 'actionpool/Thread'
2
+ require 'actionpool/Queue'
3
3
  require 'actionpool/LogHelper'
4
4
  require 'thread'
5
5
 
6
6
  module ActionPool
7
7
  class Pool
8
8
 
9
- # min_threads:: minimum number of threads in pool
10
- # max_threads:: maximum number of threads in pool
11
- # t_to:: thread timeout waiting for action to process
12
- # a_to:: maximum time action may be worked on before aborting
13
- # logger:: logger to print logging messages to
9
+ # :min_threads:: minimum number of threads in pool
10
+ # :max_threads:: maximum number of threads in pool
11
+ # :t_to:: thread timeout waiting for action to process
12
+ # :a_to:: maximum time action may be worked on before aborting
13
+ # :logger:: logger to print logging messages to
14
14
  # Creates a new pool
15
- def initialize(min_threads=10, max_threads=100, t_to=60, a_to=nil, logger=nil)
16
- @logger = LogHelper.new(logger)
17
- @queue = Queue.new
15
+ def initialize(args={})
16
+ raise ArgumentError.new('Hash required for initialization') unless args.is_a?(Hash)
17
+ @logger = LogHelper.new(args[:logger])
18
+ @queue = ActionPool::Queue.new
18
19
  @threads = []
19
- @lock = Mutex.new
20
- @thread_timeout = t_to
21
- @action_timeout = a_to
22
- @min_threads = min_threads
23
- @max_threads = max_threads
20
+ @thread_timeout = args[:t_to] ? args[:t_to] : 60
21
+ @action_timeout = args[:a_to] ? args[:a_to] : nil
22
+ @min_threads = args[:min_threads] ? args[:min_threads] : 10
23
+ @max_threads = args[:max_threads] ? args[:max_threads] : 100
24
+ @respond_to = ::Thread.current
24
25
  @min_threads.times{create_thread}
25
26
  end
26
27
 
27
28
  # force:: force creation of a new thread
28
- # Create a new thread for pool
29
+ # Create a new thread for pool. Returns newly created ActionPool::Thread or
30
+ # nil if pool has reached maximum threads
29
31
  def create_thread(force=false)
30
32
  return nil unless @threads.size < @max_threads || force
31
33
  @logger.info('Pool is creating a new thread')
32
- pt = ActionPool::Thread.new(self, @thread_timeout, @action_timeout, @logger)
34
+ pt = ActionPool::Thread.new(:pool => self, :respond_thread => @respond_to, :a_timeout => @action_timeout, :t_timeout => @thread_timeout, :logger => @logger)
33
35
  @threads << pt
34
36
  return pt
35
37
  end
@@ -38,31 +40,51 @@ module ActionPool
38
40
  # Stop the pool
39
41
  def shutdown(force=false)
40
42
  @logger.info("Pool is now shutting down #{force ? 'using force' : ''}")
41
- @threads.each{|t|t.stop}
43
+ @threads.each{|t|t.stop(force)}
42
44
  until(size < 1) do
43
45
  @queue << lambda{}
44
46
  sleep(0.1)
45
47
  end
48
+ nil
46
49
  end
47
50
 
48
51
  # action:: proc to be executed
49
52
  # Add a new proc/lambda to be executed (alias for queue)
50
53
  def <<(action)
51
54
  queue(action)
55
+ nil
52
56
  end
53
57
 
54
58
  # action:: proc to be executed
55
59
  # Add a new proc/lambda to be executed
56
60
  def queue(action)
57
- raise InvalidType.new(action.class, Proc) unless action.is_a?(Proc)
61
+ raise ArgumentError.new('Expecting block') unless action.is_a?(Proc)
58
62
  @queue << action
59
- start_thread if size > min
63
+ create_thread if @queue.length > 0 && @queue.num_waiting < 1 # only start a new thread if we need it
64
+ end
65
+
66
+ # jobs:: Array of proc/lambdas
67
+ # Will queue a list of jobs into the pool
68
+ def add_jobs(jobs)
69
+ raise ArgumentError.new("Expecting an array but received: #{jobs.class}") unless jobs.is_a?(Array)
70
+ @queue.pause
71
+ begin
72
+ jobs.each do |job|
73
+ raise ArgumentError.new('Jobs to be processed by the pool must be a proc/lambda') unless job.is_a?(Proc)
74
+ @queue << job
75
+ end
76
+ ensure
77
+ while(create_thread)do;end; # make sure we get our pool population up
78
+ @queue.unpause
79
+ end
80
+ true
60
81
  end
61
82
 
62
83
  # block:: block to process
63
84
  # Adds a block to be processed
64
85
  def process(&block)
65
86
  queue(block)
87
+ nil
66
88
  end
67
89
 
68
90
  # Current size of pool
@@ -84,23 +106,30 @@ module ActionPool
84
106
  # Set maximum number of threads
85
107
  def max=(m)
86
108
  m = m.to_i
87
- raise InvalidValue.new unless m > 0
109
+ raise ArgumentError.new('Maximum value must be greater than 0') unless m > 0
88
110
  @max_threads = m
111
+ m
89
112
  end
90
113
 
91
114
  # m:: new min
92
115
  # Set minimum number of threads
93
116
  def min=(m)
94
117
  m = m.to_i
95
- raise InvalidValue.new unless m > 0 && m <= @max_threads
118
+ raise ArgumentError.new("Minimum value must be greater than 0 and less than or equal to maximum (#{max})") unless m > 0 && m <= max
96
119
  @min_threads = m
97
120
  resize if m < size
121
+ m
98
122
  end
99
123
 
100
124
  # t:: ActionPool::Thread to remove
101
125
  # Removes a thread from the pool
102
126
  def remove(t)
103
- @threads.delete(t)
127
+ if(@threads.include?(t))
128
+ @threads.delete(t)
129
+ return true
130
+ else
131
+ return false
132
+ end
104
133
  end
105
134
 
106
135
  # Maximum number of seconds a thread
@@ -122,8 +151,9 @@ module ActionPool
122
151
  # Set maximum allowed time thead may idle in pool
123
152
  def thread_timeout=(t)
124
153
  t = to_i unless t.nil?
125
- raise InvalidValue.new unless t > 0 || t.nil?
154
+ raise ArgumentError.new('Value must be great than zero or nil') unless t.nil? || t > 0
126
155
  @thread_timeout = t
156
+ t
127
157
  end
128
158
 
129
159
  # t:: timeout in seconds (nil for infinte)
@@ -131,8 +161,9 @@ module ActionPool
131
161
  # on a given action
132
162
  def action_timeout=(t)
133
163
  t = to_i unless t.nil?
134
- raise InvalidValue.new unless t > 0 || t.nil?
164
+ raise ArgumentError.new('Value must be great than zero or nil') unless t.nil? || t > 0
135
165
  @action_timeout = t
166
+ t
136
167
  end
137
168
 
138
169
  # Returns the next action to be processed
@@ -153,6 +184,7 @@ module ActionPool
153
184
  t = @threads.shift
154
185
  t.stop
155
186
  end
187
+ nil
156
188
  end
157
189
  end
158
190
  end
@@ -0,0 +1,30 @@
1
+ require 'thread'
2
+
3
+ module ActionPool
4
+ # Adds a little bit extra functionality to the Queue class
5
+ class Queue < ::Queue
6
+ # Create a new Queue for the ActionPool::Pool
7
+ def initialize
8
+ super
9
+ @wait = false
10
+ @lock = Mutex.new
11
+ @guard = ConditionVariable.new
12
+ end
13
+ # Stop the queue from returning results to requesting
14
+ # threads. Threads will wait for results until signalled
15
+ def pause
16
+ @wait = true
17
+ end
18
+ # Allow the queue to return results. Any threads waiting
19
+ # will have results given to them.
20
+ def unpause
21
+ @wait = false
22
+ @lock.synchronize{ @guard.signal }
23
+ end
24
+ # Check if queue needs to wait before returning
25
+ def pop
26
+ @lock.synchronize{ @guard.wait(@lock) } if @wait
27
+ super
28
+ end
29
+ end
30
+ end
@@ -2,24 +2,31 @@ require 'timeout'
2
2
 
3
3
  module ActionPool
4
4
  class Thread
5
- # pool:: pool thread is associated with
6
- # t_timeout:: max time a thread is allowed to wait for action
7
- # a_timeout:: max time thread is allowed to work
8
- # logger:: LogHelper for logging messages
5
+ # :pool:: pool thread is associated with
6
+ # :t_timeout:: max time a thread is allowed to wait for action
7
+ # :a_timeout:: max time thread is allowed to work
8
+ # :respond_thread:: thread to send execptions to
9
+ # :logger:: LogHelper for logging messages
9
10
  # Create a new thread
10
- def initialize(pool, t_timeout, a_timeout, logger=nil)
11
- @pool = pool
12
- @pool_timeout = t_timeout.nil? ? 0 : t_timeout
13
- @action_timeout = a_timeout.nil? ? 0 : a_timeout
11
+ def initialize(args)
12
+ raise ArgumentError.new('Hash required for initialization') unless args.is_a?(Hash)
13
+ raise ArgumentError.new('ActionPool::Thread requires a pool') unless args[:pool]
14
+ raise ArgumentError.new('ActionPool::Thread requries thread to respond') unless args[:respond_thread]
15
+ @pool = args[:pool]
16
+ @respond_to = args[:respond_thread]
17
+ @thread_timeout = args[:t_timeout] ? args[:t_timeout] : 0
18
+ @action_timeout = args[:a_timeout] ? args[:a_timeout] : 0
14
19
  @kill = false
15
- @logger = logger
20
+ @logger = args[:logger].is_a?(LogHelper) ? args[:logger] : LogHelper.new(args[:logger])
16
21
  @thread = ::Thread.new{ start_thread }
17
22
  end
18
23
 
19
- # stop the thread
24
+ # force:: force the thread to stop
25
+ # Stop the thread
20
26
  def stop(force=false)
21
27
  @kill = true
22
28
  @thread.kill if force
29
+ nil
23
30
  end
24
31
 
25
32
  private
@@ -40,12 +47,14 @@ module ActionPool
40
47
  run(action) unless action.nil?
41
48
  rescue Timeout::Error => boom
42
49
  @kill = true
43
- rescue Object => boom
50
+ rescue Exception => boom
44
51
  @logger.error("Pool thread caught an exception: #{boom}\n#{boom.backtrace.join("\n")}")
52
+ @respond_to.raise boom
45
53
  end
46
54
  end
47
- rescue Object => boom
55
+ rescue Exception => boom
48
56
  @logger.error("Pool thread caught an exception: #{boom}\n#{boom.backtrace.join("\n")}")
57
+ @respond_to.raise boom
49
58
  ensure
50
59
  @logger.info("Pool thread is shutting down (#{self})")
51
60
  @pool.remove(self)
@@ -63,8 +72,6 @@ module ActionPool
63
72
  end
64
73
  rescue Timeout::Error => boom
65
74
  @logger.warn("Pool thread reached max execution time for action: #{boom}")
66
- rescue Object => boom
67
- @logger.error("Pool thread caught an exception running action: #{boom}\n#{boom.backtrace.join("\n")}")
68
75
  end
69
76
  end
70
77
  end
data/test.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'actionpool'
2
+
3
+ pool = ActionPool::Pool.new
4
+ a = 0
5
+ lock = Mutex.new
6
+ tasks = [].fill(lambda{ lock.synchronize{ a += 1} }, 0..19)
7
+ puts "Size: #{tasks.size}"
8
+ pool.add_jobs(tasks)
9
+ sleep(0.5)
10
+ puts "Result: #{a}"
data/test2.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'actionpool'
2
+
3
+ pool = ActionPool::Pool.new(:min_threads => 1, :max_threads => 3)
4
+ pool.process do
5
+ sleep(10)
6
+ raise 'Wakeup main thread'
7
+ end
8
+ 20.times do
9
+ pool.process do
10
+ puts "Thread: #{Thread.current}"
11
+ sleep(rand(0.0))
12
+ end
13
+ end
14
+ begin
15
+ sleep
16
+ rescue Exception => e
17
+ puts "Thread pool woke me up: #{e}"
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ActionPool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,34 +9,42 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-13 00:00:00 -07:00
12
+ date: 2009-10-13 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description:
16
+ description: ""
17
17
  email: spox@modspox.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
- extra_rdoc_files: []
23
-
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
24
  files:
25
+ - ActionPool-0.0.1.gem
26
+ - README.rdoc
25
27
  - lib
26
28
  - lib/actionpool
27
- - lib/actionpool/Exceptions.rb
28
29
  - lib/actionpool/LogHelper.rb
29
- - lib/actionpool/Pool.rb
30
30
  - lib/actionpool/Thread.rb
31
+ - lib/actionpool/Pool.rb
32
+ - lib/actionpool/Queue.rb
31
33
  - lib/actionpool.rb
32
- - README
33
- - ActionPool-0.0.1.gem
34
+ - ActionPool-0.1.0.gem
35
+ - LICENSE
36
+ - test2.rb
37
+ - test.rb
34
38
  - actionpool.gemspec
35
- has_rdoc: true
36
- homepage:
39
+ has_rdoc: false
40
+ homepage: http://github.com/spox/actionpool
37
41
  post_install_message:
38
- rdoc_options: []
39
-
42
+ rdoc_options:
43
+ - --title
44
+ - ActionPool
45
+ - --main
46
+ - README.rdoc
47
+ - --line-numbers
40
48
  require_paths:
41
49
  - lib
42
50
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -54,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
62
  requirements: []
55
63
 
56
64
  rubyforge_project:
57
- rubygems_version: 1.3.0
65
+ rubygems_version: 1.3.1
58
66
  signing_key:
59
67
  specification_version: 2
60
68
  summary: Thread Pool
data/README DELETED
@@ -1,40 +0,0 @@
1
- == ActionPool ==
2
- ~Who doesn't love a pool with action?~
3
-
4
- ActionPool is just a simple thread pool. It allows
5
- for various contraints and resizing in a pretty
6
- easy and unobtrusive manner. You can set limits
7
- on how long tasks are worked on, as well as on the
8
- life of a thread. For things that like to use lots
9
- threads, it can be helpful to reuse threads instead of
10
- constantly recreating them.
11
-
12
- install (easy):
13
-
14
- gem install ActionPool
15
-
16
- install (less easy):
17
-
18
- git clone http://github.com/spox/actionpool.git
19
- cd actionpool
20
- gem build actionpool.gemspec
21
- gem install ActionPool-x.x.x.gem
22
-
23
- It has RDocs. They are where ever your rubygems installed them.
24
- They will probably be helpful to look over.
25
-
26
- Simple example of using the pool:
27
-
28
- pool = ActionPool::Pool.new
29
- pool.process do
30
- puts "Hello world"
31
- sleep(1)
32
- puts "Goodbye world"
33
- end
34
- puts "Hello to you too"
35
-
36
- Result:
37
- Hello world
38
- Hello to you too
39
- Goodbye world
40
-
@@ -1,19 +0,0 @@
1
- module ActionPool
2
-
3
- class InvalidType < Exception
4
- attr_reader :given
5
- attr_reader :expected
6
- def initialize(g,e)
7
- @given = given
8
- @expected = e
9
- end
10
-
11
- def to_s
12
- "Given type: #{g} Expected type: #{e}"
13
- end
14
- end
15
-
16
- class InvalidValue < Exception
17
- end
18
-
19
- end