parallizer 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +6 -4
- data/lib/parallizer.rb +30 -39
- data/lib/parallizer/version.rb +1 -1
- data/lib/parallizer/worker.rb +24 -0
- data/parallizer.gemspec +1 -1
- data/spec/parallizer/proxy_spec.rb +9 -11
- data/spec/parallizer_spec.rb +3 -2
- metadata +11 -10
data/Gemfile.lock
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
parallizer (0.
|
5
|
-
|
4
|
+
parallizer (0.4.0)
|
5
|
+
celluloid (~> 0.11.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
10
|
always_execute (0.1.3)
|
11
|
+
celluloid (0.11.1)
|
12
|
+
timers (>= 1.0.0)
|
11
13
|
diff-lcs (1.1.3)
|
12
|
-
rake (0.
|
14
|
+
rake (10.0.3)
|
13
15
|
rspec (2.9.0)
|
14
16
|
rspec-core (~> 2.9.0)
|
15
17
|
rspec-expectations (~> 2.9.0)
|
@@ -18,7 +20,7 @@ GEM
|
|
18
20
|
rspec-expectations (2.9.1)
|
19
21
|
diff-lcs (~> 1.1.3)
|
20
22
|
rspec-mocks (2.9.0)
|
21
|
-
|
23
|
+
timers (1.1.0)
|
22
24
|
|
23
25
|
PLATFORMS
|
24
26
|
ruby
|
data/lib/parallizer.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require 'work_queue'
|
2
1
|
require 'parallizer/version'
|
3
2
|
require 'parallizer/proxy'
|
3
|
+
require 'parallizer/worker'
|
4
4
|
require 'parallizer/method_call_notifier'
|
5
5
|
|
6
6
|
class Parallizer
|
@@ -8,81 +8,72 @@ class Parallizer
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def work_queue_size
|
11
|
-
|
11
|
+
@work_queue_size || DEFAULT_WORK_QUEUE_SIZE
|
12
12
|
end
|
13
13
|
|
14
|
-
def work_queue_size=(
|
15
|
-
|
16
|
-
if work_queue.nil? || work_queue.max_threads != size
|
17
|
-
Thread.current[:parallizer_work_queue] = WorkQueue.new(size)
|
18
|
-
end
|
14
|
+
def work_queue_size=(work_queue_size)
|
15
|
+
@work_queue_size = work_queue_size
|
19
16
|
end
|
20
|
-
|
17
|
+
|
21
18
|
def work_queue
|
22
|
-
# TODO: share the work queue among calling threads
|
23
|
-
Thread.current[:parallizer_work_queue]
|
19
|
+
# TODO: share the work queue among calling threads??
|
20
|
+
queue = Thread.current[:parallizer_work_queue]
|
21
|
+
if queue.nil? || Thread.current[:parallizer_work_queue_size] != work_queue_size
|
22
|
+
queue = Thread.current[:parallizer_work_queue] = ::Parallizer::Worker.pool(:size => work_queue_size)
|
23
|
+
Thread.current[:parallizer_work_queue_size] = work_queue_size
|
24
|
+
end
|
25
|
+
|
26
|
+
queue
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
27
30
|
attr_reader :calls, :call_infos, :client, :proxy, :options
|
28
|
-
|
31
|
+
|
29
32
|
def initialize(client, options = {})
|
30
33
|
@client = client
|
31
34
|
@options = {:retries => 0}.merge(options)
|
32
35
|
@call_infos = {}
|
33
36
|
end
|
34
|
-
|
37
|
+
|
35
38
|
def add
|
36
|
-
MethodCallNotifier.new do |*args|
|
39
|
+
::Parallizer::MethodCallNotifier.new do |*args|
|
37
40
|
add_call(*args)
|
38
41
|
end
|
39
42
|
end
|
40
|
-
|
43
|
+
|
41
44
|
def calls
|
42
45
|
@call_infos.keys
|
43
46
|
end
|
44
|
-
|
47
|
+
|
45
48
|
def add_call(method_name, *args)
|
46
49
|
raise ArgumentError, "Cannot add calls after proxy has been generated" if @proxy
|
47
|
-
|
50
|
+
|
48
51
|
method_name_and_args = [method_name.to_sym, *args]
|
49
52
|
return if call_infos[method_name_and_args]
|
50
|
-
|
53
|
+
|
51
54
|
call_info = {
|
55
|
+
:future => ::Parallizer::work_queue.future(:run, @client, method_name, args, options),
|
52
56
|
:result => nil,
|
53
|
-
:exception => nil
|
54
|
-
:retries => options[:retries]
|
57
|
+
:exception => nil
|
55
58
|
}
|
56
59
|
call_infos[method_name_and_args] = call_info
|
57
60
|
end
|
58
|
-
|
61
|
+
|
59
62
|
def create_proxy
|
60
63
|
raise ArgumentError, "Cannot create another proxy" if @proxy
|
61
|
-
|
64
|
+
|
62
65
|
execute
|
63
|
-
|
64
|
-
Parallizer::Proxy.new(client, call_infos)
|
66
|
+
|
67
|
+
::Parallizer::Proxy.new(client, call_infos)
|
65
68
|
end
|
66
|
-
|
69
|
+
|
67
70
|
private
|
68
|
-
|
71
|
+
|
69
72
|
def execute
|
70
73
|
call_infos.each do |method_name_and_args, call_info|
|
71
|
-
|
72
|
-
(call_info[:retries] + 1).times do
|
73
|
-
begin
|
74
|
-
call_info[:exception] = nil # reset exception before each send attempt
|
75
|
-
call_info[:result] = client.send(*method_name_and_args)
|
76
|
-
break # success
|
77
|
-
rescue Exception => e
|
78
|
-
call_info[:exception] = e
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
74
|
+
call_info.merge!(call_info[:future].value)
|
82
75
|
end
|
83
|
-
|
84
|
-
Parallizer.work_queue.join
|
85
76
|
|
86
|
-
Parallizer::Proxy.new(client, call_infos)
|
77
|
+
::Parallizer::Proxy.new(client, call_infos)
|
87
78
|
end
|
88
79
|
end
|
data/lib/parallizer/version.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'celluloid'
|
2
|
+
|
3
|
+
class Parallizer
|
4
|
+
class Worker
|
5
|
+
include Celluloid
|
6
|
+
|
7
|
+
def run(client, method_name, args, options)
|
8
|
+
result = {}
|
9
|
+
|
10
|
+
(options[:retries] + 1).times do
|
11
|
+
begin
|
12
|
+
result[:exception] = nil # reset exception before each send attempt
|
13
|
+
result[:result] = client.send(method_name, *args)
|
14
|
+
break # success
|
15
|
+
rescue Exception => e
|
16
|
+
result[:exception] = e
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
data/parallizer.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_dependency '
|
21
|
+
s.add_dependency 'celluloid', '~> 0.11.0'
|
22
22
|
s.add_development_dependency 'rake'
|
23
23
|
s.add_development_dependency 'rspec', '~> 2.9.0'
|
24
24
|
s.add_development_dependency 'always_execute', '~> 0.1.1'
|
@@ -1,20 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Parallizer::Proxy do
|
4
|
-
|
4
|
+
CLIENT_RETURN_VALUE = "client return value"
|
5
5
|
|
6
|
-
class
|
6
|
+
class ClientTestObject
|
7
7
|
def a_method(arg)
|
8
|
-
|
8
|
+
CLIENT_RETURN_VALUE
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "#method_missing" do
|
13
13
|
before do
|
14
|
-
@client =
|
14
|
+
@client = ClientTestObject.new
|
15
15
|
@call_key = []
|
16
|
-
@call_info = {:result => nil, :exception => nil
|
17
|
-
:condition_variable => ConditionVariable.new, :mutex => Mutex.new }
|
16
|
+
@call_info = {:result => nil, :exception => nil }
|
18
17
|
end
|
19
18
|
|
20
19
|
execute do
|
@@ -43,7 +42,7 @@ describe Parallizer::Proxy do
|
|
43
42
|
end
|
44
43
|
a_calling_thread_method
|
45
44
|
end
|
46
|
-
|
45
|
+
|
47
46
|
it "should raise exception" do
|
48
47
|
@execute_result.class.should == @call_info[:exception].class
|
49
48
|
@execute_result.message.should == @call_info[:exception].message
|
@@ -73,7 +72,7 @@ describe Parallizer::Proxy do
|
|
73
72
|
end
|
74
73
|
|
75
74
|
it "should return value from client object" do
|
76
|
-
@execute_result.should ==
|
75
|
+
@execute_result.should == CLIENT_RETURN_VALUE
|
77
76
|
end
|
78
77
|
end
|
79
78
|
end
|
@@ -92,10 +91,9 @@ describe Parallizer::Proxy do
|
|
92
91
|
|
93
92
|
describe "#respond_to?" do
|
94
93
|
before do
|
95
|
-
client =
|
94
|
+
client = ClientTestObject.new
|
96
95
|
call_key = [:a_method, 'valid argument']
|
97
|
-
call_info = {:result => nil, :exception => nil
|
98
|
-
:condition_variable => ConditionVariable.new, :mutex => Mutex.new }
|
96
|
+
call_info = {:result => nil, :exception => nil }
|
99
97
|
@proxy = Parallizer::Proxy.new(client, { call_key => call_info })
|
100
98
|
end
|
101
99
|
|
data/spec/parallizer_spec.rb
CHANGED
@@ -191,10 +191,11 @@ describe Parallizer do
|
|
191
191
|
Parallizer.work_queue_size.should == Parallizer::DEFAULT_WORK_QUEUE_SIZE
|
192
192
|
end
|
193
193
|
|
194
|
-
it "should have max threads equal to specified size" do
|
194
|
+
it "should have max threads equal to specified size after requesting the work queue" do
|
195
195
|
size = rand(2..5)
|
196
196
|
Parallizer.work_queue_size = size
|
197
|
-
Parallizer.work_queue
|
197
|
+
Parallizer.work_queue
|
198
|
+
Thread.current[:parallizer_work_queue_size].should == size
|
198
199
|
end
|
199
200
|
end
|
200
201
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parallizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: celluloid
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 0.11.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 0.11.0
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rake
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/parallizer/method_call_notifier.rb
|
93
93
|
- lib/parallizer/proxy.rb
|
94
94
|
- lib/parallizer/version.rb
|
95
|
+
- lib/parallizer/worker.rb
|
95
96
|
- parallizer.gemspec
|
96
97
|
- spec/parallizer/method_call_notifier_spec.rb
|
97
98
|
- spec/parallizer/proxy_spec.rb
|
@@ -111,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
112
|
version: '0'
|
112
113
|
segments:
|
113
114
|
- 0
|
114
|
-
hash:
|
115
|
+
hash: -3493791193896419767
|
115
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
117
|
none: false
|
117
118
|
requirements:
|
@@ -120,10 +121,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
121
|
version: '0'
|
121
122
|
segments:
|
122
123
|
- 0
|
123
|
-
hash:
|
124
|
+
hash: -3493791193896419767
|
124
125
|
requirements: []
|
125
126
|
rubyforge_project: parallizer
|
126
|
-
rubygems_version: 1.8.
|
127
|
+
rubygems_version: 1.8.25
|
127
128
|
signing_key:
|
128
129
|
specification_version: 3
|
129
130
|
summary: Execute your service layer in parallel
|