tubes 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/tubes.rb +32 -23
  2. metadata +2 -2
@@ -35,6 +35,17 @@ class Tube
35
35
  @invocations = 0
36
36
 
37
37
  Dir.mkdir(@dir) unless Dir.exists?(@dir)
38
+
39
+ if @parent.nil? # This is the top level tube.
40
+ class << self
41
+ alias_method :unlocked_run, :run
42
+ def run(*args, &block)
43
+ lock
44
+ unlocked_run(*args, &block)
45
+ unlock
46
+ end
47
+ end
48
+ end
38
49
  end
39
50
 
40
51
 
@@ -48,26 +59,6 @@ class Tube
48
59
  end
49
60
 
50
61
 
51
- def start
52
- lock = File.join @dir, "lock"
53
- if !@options[:force] && File.exists?(lock)
54
- raise "Another instance of the tubes seems to be running.\nPlease remove #{lock} if that is not the case."
55
- end
56
-
57
- File.open(lock, "w") do |f|
58
- f.write $$
59
- end
60
- end
61
-
62
-
63
- def finish
64
- lock = File.join @dir, "lock"
65
- File.delete lock
66
-
67
- @ended_at = Time.now
68
- end
69
-
70
-
71
62
  def invoke(klass, *args)
72
63
  @invocations += 1
73
64
 
@@ -85,14 +76,14 @@ class Tube
85
76
  self.puts "Running: #{step}"
86
77
 
87
78
  if @type == :serial
88
- run(segment, output_file, *args)
79
+ dispatch(segment, output_file, *args)
89
80
  elsif @type == :parallel
90
81
  thread = Thread.new(@lock) do |lock|
91
82
  Thread.current[:lock] = lock
92
83
  Thread.current.abort_on_exception = true
93
84
 
94
85
  # This clobbers the @output. Perhaps make @output an array instead of a value and append to it under a lock.
95
- run(segment, output_file, *args)
86
+ dispatch(segment, output_file, *args)
96
87
  end
97
88
  @threads << thread
98
89
  end
@@ -143,7 +134,7 @@ class Tube
143
134
  # This should be implemented in the subclasses.
144
135
  end
145
136
 
146
- def run(segment, output_file, *args)
137
+ def dispatch(segment, output_file, *args)
147
138
  output = if segment.method(:run).arity.abs > 0 # Optional arguments result in negative arity.
148
139
  if args.empty?
149
140
  segment.send :run, @output
@@ -196,4 +187,22 @@ class Tube
196
187
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
197
188
  tr("-", "_").downcase
198
189
  end
190
+
191
+ def lock
192
+ lock = File.join @dir, "lock"
193
+ if !@options[:force] && File.exists?(lock)
194
+ raise "Another instance of the tubes seems to be running.\nPlease remove #{lock} if that is not the case."
195
+ end
196
+
197
+ File.open(lock, "w") do |f|
198
+ f.write $$
199
+ end
200
+ end
201
+
202
+ def unlock
203
+ lock = File.join @dir, "lock"
204
+ File.delete lock
205
+
206
+ @ended_at = Time.now
207
+ end
199
208
  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.0.0
4
+ version: 0.1.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: 2012-09-21 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple way to build a pipeline of tasks.
15
15
  email: sujoyg@gmail.com