tubes 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tubes.rb +26 -11
  2. metadata +2 -2
data/lib/tubes.rb CHANGED
@@ -13,8 +13,11 @@ class Tube
13
13
  attr :thread_lock
14
14
  attr :threads
15
15
 
16
- def initialize(dir=nil, options={})
17
- @dir = dir || File.join('/tmp', Time.now.strftime('%Y-%m-%d_%H:%M:%S'))
16
+ def initialize(*args)
17
+ options = args.last.is_a?(Hash) ? args.pop : {}
18
+ dir = args.first
19
+
20
+ @dir = dir
18
21
  @type = options.delete(:type) || :serial
19
22
  @parent = options.delete(:parent) # This is nil only for the top level tube.
20
23
 
@@ -38,7 +41,9 @@ class Tube
38
41
  @started_at = options[:started_at] || Time.now
39
42
  @invocations = 0
40
43
 
41
- Dir.mkdir(@dir) unless Dir.exists?(@dir)
44
+ unless @dir.nil?
45
+ Dir.mkdir(@dir) unless Dir.exists?(@dir)
46
+ end
42
47
 
43
48
  if @parent.nil? # This is the top level tube.
44
49
  class << self
@@ -74,7 +79,7 @@ class Tube
74
79
  step = segment.name
75
80
 
76
81
  output_file = segment_cache self, step
77
- if File.exists?(output_file)
82
+ if output_file && File.exists?(output_file)
78
83
  self.puts "Skipping: #{step}"
79
84
  output = JSON.load(File.read(output_file))["data"]
80
85
 
@@ -165,6 +170,10 @@ class Tube
165
170
  # This should be implemented in the subclasses.
166
171
  end
167
172
 
173
+ def run
174
+ # This should be implemented in the subclasses.
175
+ end
176
+
168
177
  def dispatch(segment, output_file, *args)
169
178
  output = if segment.method(:run).arity > 0 # Optional arguments result in negative arity.
170
179
  if args.empty?
@@ -190,8 +199,10 @@ class Tube
190
199
  segment.send :run
191
200
  end
192
201
 
193
- File.open(output_file, "w") do |f|
194
- f.write({:data => output}.to_json)
202
+ if output_file
203
+ File.open(output_file, "w") do |f|
204
+ f.write({:data => output}.to_json)
205
+ end
195
206
  end
196
207
 
197
208
  if serial?
@@ -205,7 +216,7 @@ class Tube
205
216
 
206
217
 
207
218
  def segment_cache(tube, segment)
208
- File.join tube.dir, "#{tube.order}-#{@invocations}-#{segment}.json"
219
+ File.join(tube.dir, "#{tube.order}-#{@invocations}-#{segment}.json") if tube.dir
209
220
  end
210
221
 
211
222
 
@@ -229,19 +240,23 @@ class Tube
229
240
  end
230
241
 
231
242
  def lock
232
- lock = File.join @dir, "lock"
243
+ return if @dir.nil?
244
+
245
+ lock = File.join @dir, 'lock'
233
246
  if !@options[:force] && File.exists?(lock)
234
247
  raise "Another instance of the tubes seems to be running.\nPlease remove #{lock} if that is not the case."
235
248
  end
236
249
 
237
- File.open(lock, "w") do |f|
250
+ File.open(lock, 'w') do |f|
238
251
  f.write $$
239
252
  end
240
253
  end
241
254
 
242
255
  def unlock
243
- lock = File.join @dir, "lock"
244
- File.delete lock
256
+ unless @dir.nil?
257
+ lock = File.join @dir, 'lock'
258
+ File.delete lock
259
+ end
245
260
 
246
261
  @ended_at = Time.now
247
262
  end
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.8
4
+ version: 0.1.9
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-10-02 00:00:00.000000000 Z
12
+ date: 2012-10-08 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.