tpool 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/tpool.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require "thread"
2
2
 
3
3
  class Tpool
4
+ attr_reader :args
5
+
6
+ ARGS_VALID = [:threads, :priority, :on_error]
7
+
4
8
  def self.const_missing(name)
5
9
  require "#{File.dirname(__FILE__)}/tpool_#{name.to_s.downcase}.rb"
6
10
  raise "Still not defined: '#{name}'." if !Tpool.const_defined?(name)
@@ -15,10 +19,15 @@ class Tpool
15
19
 
16
20
  def initialize(args)
17
21
  @args = args
22
+ @args.each do |key, val|
23
+ raise "Invalid argument given: '#{key}'." if Tpool::ARGS_VALID.index(key) == nil
24
+ end
25
+
18
26
  @queue = Queue.new
19
27
  self.start
20
28
  end
21
29
 
30
+ #Starts the thread-pool. This is automatically called on initialization.
22
31
  def start
23
32
  raise "Already started." if @pool and !@pool.empty?
24
33
 
@@ -57,6 +66,7 @@ class Tpool
57
66
  block = Tpool::Block.new(
58
67
  :args => args,
59
68
  :blk => blk,
69
+ :tpool => self,
60
70
  :thread_starts => [Thread.current]
61
71
  )
62
72
  @queue << block
data/lib/tpool_block.rb CHANGED
@@ -5,6 +5,7 @@ class Tpool::Block
5
5
  def initialize(args)
6
6
  @args = args
7
7
 
8
+ @tpool = @args[:tpool]
8
9
  @running = false
9
10
  @done = false
10
11
  @error = nil
@@ -13,13 +14,14 @@ class Tpool::Block
13
14
  #Starts running whatever block it is holding.
14
15
  def run
15
16
  @thread_running = Thread.current
17
+ @thread_running.priority = @tpool.args[:priority] if @tpool.args.key?(:priority)
16
18
  @running = true
17
19
 
18
20
  begin
19
21
  @res = @args[:blk].call(*@args[:args], &@args[:blk])
20
22
  rescue Exception => e
21
23
  @error = e
22
- @args[:tpool].on_error_call(:on_error, e)
24
+ @args[:tpool].on_error_call(:error => e, :block => self)
23
25
  ensure
24
26
  @running = false
25
27
  @done = true
data/spec/tpool_spec.rb CHANGED
@@ -2,11 +2,22 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Tpool" do
4
4
  it "should work" do
5
- tp = Tpool.new(:threads => 4)
5
+ errs = []
6
+
7
+ tp = Tpool.new(
8
+ :threads => 4,
9
+ :priority => -3,
10
+ :on_error => lambda{|data|
11
+ errs << data[:error]
12
+ }
13
+ )
14
+
6
15
  res = tp.run do
16
+ errs << "Expected thread priority to be -3 but it wasnt: '#{Thread.current.priority}'." if Thread.current.priority != -3
7
17
  "kasper"
8
18
  end
9
19
 
20
+ raise errs.first if !errs.empty?
10
21
  raise "Expected result to be 'kasper' but it wasnt: '#{res}'." if res != "kasper"
11
22
 
12
23
 
@@ -34,5 +45,7 @@ describe "Tpool" do
34
45
  end
35
46
 
36
47
  raise "Expected error but didnt get one." if !error
48
+
49
+ raise errs.first if !errs.empty?
37
50
  end
38
51
  end
data/tpool.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tpool}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-08-19}
12
+ s.date = %q{2012-08-27}
13
13
  s.description = %q{A thread-pool for Ruby that supports joining, status-checking, stopping jobs and more.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: tpool
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-08-19 00:00:00 +02:00
13
+ date: 2012-08-27 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- hash: 733002826955008460
97
+ hash: -3322771653679867598
98
98
  segments:
99
99
  - 0
100
100
  version: "0"