tubes 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tubes.rb +29 -22
- metadata +3 -3
data/lib/tubes.rb
CHANGED
@@ -15,7 +15,7 @@ class Tube
|
|
15
15
|
def initialize(dir=nil, options={})
|
16
16
|
@dir = dir || File.join('/tmp', Time.now.strftime('%Y-%m-%d_%H:%M:%S'))
|
17
17
|
@type = options[:type] || :serial
|
18
|
-
@parent = options[:parent]
|
18
|
+
@parent = options[:parent] # This is nil only for the top level tube.
|
19
19
|
@serial_count = 0
|
20
20
|
@parallel_count = 0
|
21
21
|
|
@@ -36,15 +36,16 @@ class Tube
|
|
36
36
|
|
37
37
|
Dir.mkdir(@dir) unless Dir.exists?(@dir)
|
38
38
|
|
39
|
-
if @parent.nil?
|
39
|
+
if @parent.nil? # This is the top level tube.
|
40
40
|
class << self
|
41
41
|
alias_method :unlocked_run, :run
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
|
43
|
+
def run(*args, &block)
|
44
|
+
lock
|
45
|
+
unlocked_run(*args, &block)
|
46
|
+
unlock
|
47
|
+
notify
|
48
|
+
end
|
48
49
|
end
|
49
50
|
end
|
50
51
|
end
|
@@ -82,7 +83,6 @@ class Tube
|
|
82
83
|
Thread.current[:lock] = lock
|
83
84
|
Thread.current.abort_on_exception = true
|
84
85
|
|
85
|
-
# This clobbers the @output. Perhaps make @output an array instead of a value and append to it under a lock.
|
86
86
|
dispatch(segment, output_file, *args)
|
87
87
|
end
|
88
88
|
@threads << thread
|
@@ -96,9 +96,9 @@ class Tube
|
|
96
96
|
def puts(string="")
|
97
97
|
@thread_lock.synchronize do
|
98
98
|
if self.class == Tube
|
99
|
-
|
99
|
+
Kernel.puts "\033[32m[#{@order}]\033[0m #{string}"
|
100
100
|
else
|
101
|
-
|
101
|
+
Kernel.puts "\033[32m[#{@order}]\033[0m\033[36m[#{@name}]\033[0m #{string}"
|
102
102
|
end
|
103
103
|
|
104
104
|
STDOUT.flush
|
@@ -115,13 +115,20 @@ class Tube
|
|
115
115
|
thread = Thread.new(@thread_lock) do |lock|
|
116
116
|
Thread.current[:lock] = lock
|
117
117
|
Thread.current.abort_on_exception = true
|
118
|
-
|
118
|
+
|
119
|
+
tube = child(mode, args)
|
120
|
+
tube.instance_eval &block
|
121
|
+
tube.threads.each { |thread| thread.join } # Could be a parallel block inside a parallel block.
|
122
|
+
@thread_lock.synchronize do
|
123
|
+
@output << mode == :parallel ? tube.output.flatten(1) : tube.output
|
124
|
+
end
|
119
125
|
end
|
120
126
|
@threads << thread
|
121
127
|
when :serial # When inside serial.
|
122
128
|
tube = child(mode, args)
|
123
129
|
tube.instance_eval &block
|
124
130
|
tube.threads.each { |thread| thread.join }
|
131
|
+
@output = mode == :parallel ? tube.output.flatten(1) : tube.output
|
125
132
|
end
|
126
133
|
rescue => e
|
127
134
|
@exception = e
|
@@ -170,22 +177,22 @@ class Tube
|
|
170
177
|
output = args || @output
|
171
178
|
|
172
179
|
order = case type
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
+
when :serial
|
181
|
+
@serial_count += 1
|
182
|
+
"#{@order}S#{@serial_count}"
|
183
|
+
when :parallel
|
184
|
+
@parallel_count += 1
|
185
|
+
"#{@order}P#{@parallel_count}"
|
186
|
+
end
|
180
187
|
|
181
188
|
Tube.new(@dir, :type => type, :output => output, :parent => self, :order => order, :started_at => started_at)
|
182
189
|
end
|
183
190
|
|
184
191
|
def underscore(string)
|
185
192
|
string.gsub(/::/, '/').
|
186
|
-
|
187
|
-
|
188
|
-
|
193
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
194
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
195
|
+
tr("-", "_").downcase
|
189
196
|
end
|
190
197
|
|
191
198
|
def lock
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tubes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
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: 2012-09-
|
12
|
+
date: 2012-09-29 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple way to build a pipeline of tasks. These tasks can be configured
|
15
15
|
to run in serial, parallel or any combination thereof.
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
41
|
rubyforge_project:
|
42
|
-
rubygems_version: 1.8.
|
42
|
+
rubygems_version: 1.8.24
|
43
43
|
signing_key:
|
44
44
|
specification_version: 3
|
45
45
|
summary: Tubes
|