vuf 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vuf/batch.rb +6 -6
- data/lib/vuf/pool.rb +25 -0
- data/lib/vuf/version.rb +1 -1
- data/lib/vuf/working_pool.rb +19 -13
- data/lib/vuf.rb +1 -0
- data/test/lib/vuf/wp_test.rb +11 -0
- metadata +2 -2
data/lib/vuf/batch.rb
CHANGED
@@ -2,13 +2,14 @@ module Vuf
|
|
2
2
|
class Batch
|
3
3
|
def initialize(size,&batch)
|
4
4
|
@mutex = Mutex.new
|
5
|
-
@batchQ =
|
5
|
+
@batchQ = []
|
6
|
+
@size = size
|
6
7
|
@batch = batch
|
7
8
|
end
|
8
9
|
|
9
10
|
def push(obj)
|
10
|
-
@batchQ
|
11
|
-
objsToProc = get_objs(@
|
11
|
+
@mutex.synchronize { @batchQ << obj }
|
12
|
+
objsToProc = get_objs(@size)
|
12
13
|
@batch.call(objsToProc) unless objsToProc.nil?
|
13
14
|
end
|
14
15
|
|
@@ -22,9 +23,8 @@ module Vuf
|
|
22
23
|
objToProc = nil
|
23
24
|
@mutex.synchronize do
|
24
25
|
if size_condition <= @batchQ.size
|
25
|
-
objToProc =
|
26
|
-
|
27
|
-
@batchQ.clear
|
26
|
+
objToProc = @batchQ
|
27
|
+
@batchQ = []
|
28
28
|
end
|
29
29
|
end
|
30
30
|
return objToProc
|
data/lib/vuf/pool.rb
CHANGED
@@ -63,6 +63,31 @@ module Vuf
|
|
63
63
|
end
|
64
64
|
!instance__.nil?
|
65
65
|
end
|
66
|
+
|
67
|
+
def klass.count
|
68
|
+
count = 0
|
69
|
+
@pool__mutex__.synchronize do
|
70
|
+
count = @pool__instances__.size + @used__instances__.size
|
71
|
+
end
|
72
|
+
return count
|
73
|
+
end
|
74
|
+
|
75
|
+
def klass.running
|
76
|
+
count = 0
|
77
|
+
@pool__mutex__.synchronize do
|
78
|
+
count = @used__instances__.size
|
79
|
+
end
|
80
|
+
return count
|
81
|
+
end
|
82
|
+
|
83
|
+
def klass.finalize # :nodoc:
|
84
|
+
@pool__mutex__.synchronize do
|
85
|
+
until @pool__instances__.empty?
|
86
|
+
i = @pool__instances__.pop
|
87
|
+
i.finalize if i.respond_to?(:finalize)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
66
91
|
|
67
92
|
klass
|
68
93
|
end
|
data/lib/vuf/version.rb
CHANGED
data/lib/vuf/working_pool.rb
CHANGED
@@ -25,8 +25,8 @@ module Vuf
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def do(channel=nil
|
29
|
-
@wq.push([channel,task])
|
28
|
+
def do(channel=nil, *args, &task)
|
29
|
+
@wq.push([channel,task,args])
|
30
30
|
end
|
31
31
|
|
32
32
|
def finalize
|
@@ -37,19 +37,21 @@ module Vuf
|
|
37
37
|
@workers.each do |worker|
|
38
38
|
worker.join
|
39
39
|
end
|
40
|
+
@workers=nil
|
40
41
|
end
|
41
42
|
|
42
43
|
private
|
43
44
|
|
44
|
-
def try_lock_channel(channel,task)
|
45
|
+
def try_lock_channel(channel,task, args)
|
45
46
|
new_channel_q = nil
|
46
47
|
@channels_mutex.synchronize {
|
47
48
|
if @channels[channel].nil?
|
48
49
|
new_channel_q = @channelsQ.shift
|
50
|
+
raise "Missing queue in working pool" unless new_channel_q.instance_of?(Queue)
|
49
51
|
@channels[channel]=new_channel_q
|
50
52
|
end
|
53
|
+
@channels[channel].push([task, args])
|
51
54
|
}
|
52
|
-
@channels[channel].push(task)
|
53
55
|
return new_channel_q
|
54
56
|
end
|
55
57
|
|
@@ -64,16 +66,20 @@ module Vuf
|
|
64
66
|
|
65
67
|
def works
|
66
68
|
task=nil
|
67
|
-
until ENDING_TASK == (task= @wq.pop)
|
68
|
-
channel = task.first
|
69
|
-
task = task.last
|
69
|
+
until ENDING_TASK == (channel, task, args = @wq.pop)
|
70
70
|
channelQ = nil
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
71
|
+
channelQ = try_lock_channel(channel, task, args) unless channel.nil?
|
72
|
+
unless channelQ.nil?
|
73
|
+
until is_clear(channel)
|
74
|
+
task,args = channelQ.pop
|
75
|
+
begin
|
76
|
+
task.call(*args)
|
77
|
+
rescue => e
|
78
|
+
Logger.error "Worker catch a task exception :\n#{e.message}\n#{e.backtrace.join("\n")}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
channelQ = nil
|
82
|
+
end
|
77
83
|
end
|
78
84
|
end
|
79
85
|
end
|
data/lib/vuf.rb
CHANGED
data/test/lib/vuf/wp_test.rb
CHANGED
@@ -26,5 +26,16 @@ describe Vuf::WorkingPool do
|
|
26
26
|
end
|
27
27
|
subject.finalize
|
28
28
|
end
|
29
|
+
|
30
|
+
it "must handle do with params and channel without error" do
|
31
|
+
subject.run
|
32
|
+
5.times do |i|
|
33
|
+
subject.do(nil,i) { |var|
|
34
|
+
sleep(var/10)
|
35
|
+
}
|
36
|
+
end
|
37
|
+
subject.finalize
|
38
|
+
end
|
39
|
+
|
29
40
|
end
|
30
41
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vuf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|