workqueue 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/workqueue.rb +26 -14
- data/lib/workqueue/version.rb +1 -1
- metadata +3 -3
data/lib/workqueue.rb
CHANGED
@@ -4,15 +4,11 @@ class WorkQueue
|
|
4
4
|
class ThreadsafeCounter
|
5
5
|
def initialize(count=0)
|
6
6
|
@count = count
|
7
|
+
@mutex = Mutex.new
|
7
8
|
end
|
8
9
|
|
9
10
|
def incr(by=1)
|
10
|
-
mutex.synchronize { @count += by }
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
def mutex
|
15
|
-
@mutex ||= Mutex.new
|
11
|
+
@mutex.synchronize { @count += by }
|
16
12
|
end
|
17
13
|
end
|
18
14
|
|
@@ -21,6 +17,7 @@ class WorkQueue
|
|
21
17
|
def initialize(init_queue=[], opts={}, &job)
|
22
18
|
@job = job
|
23
19
|
@queue = Queue.new
|
20
|
+
@mutex = Mutex.new
|
24
21
|
|
25
22
|
opts.each { |k, v| send(:"#{k}=", v) }
|
26
23
|
|
@@ -32,22 +29,33 @@ class WorkQueue
|
|
32
29
|
@size ||= 2
|
33
30
|
end
|
34
31
|
|
35
|
-
def workers
|
36
|
-
@workers ||= []
|
37
|
-
end
|
38
|
-
|
39
32
|
def work!
|
40
|
-
|
33
|
+
loop do
|
41
34
|
begin
|
42
|
-
|
35
|
+
# initialize these here so they can be set in
|
36
|
+
# the synchronize block
|
37
|
+
payload, index = []
|
38
|
+
|
39
|
+
@mutex.synchronize {
|
40
|
+
unless @aborted or (@joined and queue.empty?)
|
41
|
+
payload, index = queue.shift
|
42
|
+
end
|
43
|
+
}
|
44
|
+
|
45
|
+
break if payload.nil?
|
46
|
+
|
43
47
|
aggregate[index] = job.call(payload)
|
44
48
|
rescue Exception
|
45
|
-
|
49
|
+
abort!
|
46
50
|
raise
|
47
51
|
end
|
48
52
|
end
|
49
53
|
end
|
50
54
|
|
55
|
+
def abort!
|
56
|
+
@aborted = true
|
57
|
+
end
|
58
|
+
|
51
59
|
def run
|
52
60
|
@workers = (1..size).map do
|
53
61
|
Thread.new { work! }
|
@@ -57,7 +65,7 @@ class WorkQueue
|
|
57
65
|
end
|
58
66
|
|
59
67
|
def push(e)
|
60
|
-
|
68
|
+
queue.push([e, cursor.incr])
|
61
69
|
|
62
70
|
self
|
63
71
|
end
|
@@ -87,4 +95,8 @@ private
|
|
87
95
|
def cursor
|
88
96
|
@cursor ||= ThreadsafeCounter.new(-1)
|
89
97
|
end
|
98
|
+
|
99
|
+
def workers
|
100
|
+
@workers ||= []
|
101
|
+
end
|
90
102
|
end
|
data/lib/workqueue/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: workqueue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -38,7 +38,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
38
|
version: '0'
|
39
39
|
segments:
|
40
40
|
- 0
|
41
|
-
hash: -
|
41
|
+
hash: -1761209356095410953
|
42
42
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
43
|
none: false
|
44
44
|
requirements:
|
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
47
|
version: '0'
|
48
48
|
segments:
|
49
49
|
- 0
|
50
|
-
hash: -
|
50
|
+
hash: -1761209356095410953
|
51
51
|
requirements: []
|
52
52
|
rubyforge_project: workqueue
|
53
53
|
rubygems_version: 1.8.10
|