workqueue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'minitest'
6
+ gem 'wrong'
7
+ gem 'rake'
@@ -0,0 +1,5 @@
1
+ class WorkQueue
2
+ def self.version
3
+ '0.0.1'
4
+ end
5
+ end
data/lib/workqueue.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'thread'
2
+
3
+ class WorkQueue
4
+ class ThreadsafeCounter
5
+ def initialize(count=0)
6
+ @count = count
7
+ end
8
+
9
+ def incr(by=1)
10
+ mutex.synchronize { @count += by }
11
+ end
12
+
13
+ private
14
+ def mutex
15
+ @mutex ||= Mutex.new
16
+ end
17
+ end
18
+
19
+ attr_reader :queue
20
+ attr_reader :job
21
+ def initialize(init_queue=[], opts={}, &job)
22
+ @job = job
23
+ @queue = Queue.new
24
+
25
+ opts.each { |k, v| send(:"#{k}=", v) }
26
+
27
+ concat(init_queue)
28
+ end
29
+
30
+ attr_writer :size
31
+ def size
32
+ @size ||= 2
33
+ end
34
+
35
+ def workers
36
+ @workers ||= []
37
+ end
38
+
39
+ def work!
40
+ until @aborted or (@joined and queue.empty?)
41
+ begin
42
+ payload, index = queue.shift
43
+ aggregate[index] = job.call(payload)
44
+ rescue Exception
45
+ @aborted = true
46
+ raise
47
+ end
48
+ end
49
+ end
50
+
51
+ def run
52
+ @workers ||= (1..size).map do
53
+ Thread.new { work! }
54
+ end
55
+
56
+ self
57
+ end
58
+
59
+ def push(e)
60
+ @queue.push([e, cursor.incr])
61
+
62
+ self
63
+ end
64
+ alias << push
65
+
66
+ def concat(arr)
67
+ arr.each { |x| push(x) }
68
+ end
69
+
70
+ def join
71
+ @joined = true
72
+ workers.each(&:join)
73
+
74
+ self
75
+ end
76
+
77
+ def results
78
+ join
79
+ aggregate
80
+ end
81
+
82
+ private
83
+ def aggregate
84
+ @aggregator ||= []
85
+ end
86
+
87
+ def cursor
88
+ @cursor ||= ThreadsafeCounter.new(-1)
89
+ end
90
+ end
data/workqueue.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ require './lib/workqueue/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "workqueue"
5
+ s.version = WorkQueue.version
6
+ s.authors = ["Jay Adkisson"]
7
+ s.email = ["jay@goodguide.com"]
8
+ s.summary = "A dirt simple workqueue library for Ruby ~> 1.9"
9
+ s.description = <<-desc
10
+ A description will be forthcoming
11
+ desc
12
+
13
+ s.homepage = "http://github.com/jayferd/workqueue"
14
+ s.rubyforge_project = "workqueue"
15
+ s.files = Dir['Gemfile', 'workqueue.gemspec', 'lib/**/*.rb']
16
+
17
+ # no dependencies
18
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workqueue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jay Adkisson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-08 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ! ' A description will be forthcoming
15
+
16
+ '
17
+ email:
18
+ - jay@goodguide.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - Gemfile
24
+ - workqueue.gemspec
25
+ - lib/workqueue.rb
26
+ - lib/workqueue/version.rb
27
+ homepage: http://github.com/jayferd/workqueue
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ segments:
40
+ - 0
41
+ hash: -901472412083853702
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ segments:
49
+ - 0
50
+ hash: -901472412083853702
51
+ requirements: []
52
+ rubyforge_project: workqueue
53
+ rubygems_version: 1.8.10
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: A dirt simple workqueue library for Ruby ~> 1.9
57
+ test_files: []