Class: Step

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/workflow/step.rb,
lib/rbbt/workflow/accessor.rb,
lib/rbbt/workflow/step/run.rb,
lib/rbbt/workflow/step/dependencies.rb

Direct Known Subclasses

WorkflowRESTClient::RemoteStep

Constant Summary collapse

INFO_SERIALIAZER =
Marshal
STREAM_CACHE =
{}
STREAM_CACHE_MUTEX =
Mutex.new

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, task = nil, inputs = nil, dependencies = nil, bindings = nil, clean_name = nil) ⇒ Step

Returns a new instance of Step.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbbt/workflow/step.rb', line 31

def initialize(path, task = nil, inputs = nil, dependencies = nil, bindings = nil, clean_name = nil)
  path = Path.setup(Misc.sanitize_filename(path)) if String === path
  path = path.call if Proc === path

  @path = path
  @task = task
  @bindings = bindings
  @dependencies = case
                  when dependencies.nil? 
                    []
                  when Array === dependencies
                    dependencies
                  else
                    [dependencies]
                  end
  @mutex = Mutex.new
  @info_mutex = Mutex.new
  @inputs = inputs || []
  NamedArray.setup @inputs, task.inputs.collect{|s| s.to_s} if task and task.respond_to? :inputs and task.inputs
end

Class Attribute Details

.lock_dirObject

Returns the value of attribute lock_dir.



14
15
16
# File 'lib/rbbt/workflow/step.rb', line 14

def lock_dir
  @lock_dir
end

.log_relay_stepObject

Returns the value of attribute log_relay_step.



77
78
79
# File 'lib/rbbt/workflow/step.rb', line 77

def log_relay_step
  @log_relay_step
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



8
9
10
# File 'lib/rbbt/workflow/step.rb', line 8

def bindings
  @bindings
end

#clean_nameObject

Returns the value of attribute clean_name.



8
9
10
# File 'lib/rbbt/workflow/step.rb', line 8

def clean_name
  @clean_name
end

#dependenciesObject

Returns the value of attribute dependencies.



8
9
10
# File 'lib/rbbt/workflow/step.rb', line 8

def dependencies
  @dependencies
end

#duppedObject (readonly)

Returns the value of attribute dupped.



5
6
7
# File 'lib/rbbt/workflow/step/run.rb', line 5

def dupped
  @dupped
end

#exec(no_load = false) ⇒ Object

Returns the value of attribute exec.



10
11
12
# File 'lib/rbbt/workflow/step.rb', line 10

def exec
  @exec
end

#inputsObject

Returns the value of attribute inputs.



8
9
10
# File 'lib/rbbt/workflow/step.rb', line 8

def inputs
  @inputs
end

#mutexObject

Returns the value of attribute mutex.



11
12
13
# File 'lib/rbbt/workflow/step.rb', line 11

def mutex
  @mutex
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/rbbt/workflow/step.rb', line 8

def path
  @path
end

#pidObject

Returns the value of attribute pid.



9
10
11
# File 'lib/rbbt/workflow/step.rb', line 9

def pid
  @pid
end

#resultObject

Returns the value of attribute result.



11
12
13
# File 'lib/rbbt/workflow/step.rb', line 11

def result
  @result
end

#saved_streamObject (readonly)

Returns the value of attribute saved_stream.



5
6
7
# File 'lib/rbbt/workflow/step/run.rb', line 5

def saved_stream
  @saved_stream
end

#seenObject

Returns the value of attribute seen.



11
12
13
# File 'lib/rbbt/workflow/step.rb', line 11

def seen
  @seen
end

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/rbbt/workflow/step/run.rb', line 5

def stream
  @stream
end

#taskObject

Returns the value of attribute task.



8
9
10
# File 'lib/rbbt/workflow/step.rb', line 8

def task
  @task
end

Class Method Details

.clean(path) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rbbt/workflow/step.rb', line 202

def self.clean(path)
  info_file = Step.info_file path
  pid_file = Step.pid_file path
  files_dir = Step.files_dir path

  if Open.exists?(path) or Open.exists?(pid_file) or Open.exists?(info_file)

    @result = nil
    @pid = nil

    Misc.insist do
      Open.rm info_file if Open.exists? info_file
      #Open.rm info_file + '.lock' if Open.exists? info_file + '.lock'
      Open.rm path if Open.exists? path
      #Open.rm path + '.lock' if Open.exists? path + '.lock'
      Open.rm_rf files_dir if Open.exists? files_dir
      Open.rm pid_file if Open.exists? pid_file
    end
  end
end

.dup_stream(stream) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbbt/workflow/step/dependencies.rb', line 18

def self.dup_stream(stream)
  case stream
  when IO, File, Step
    return stream if stream.respond_to?(:closed?) and stream.closed?
    return stream if stream.respond_to?(:done?) and stream.done?

    STREAM_CACHE_MUTEX.synchronize do
      stream_key = Misc.fingerprint(stream)
      current = STREAM_CACHE[stream_key]
      case current
      when nil, Step
        Log.medium "Not duplicating stream #{stream_key}"
        STREAM_CACHE[stream_key] = stream
      when File
        if Open.exists? current.path 
          Log.medium "Reopening file #{stream_key}"
          Open.open(current.path)
        else
          new = Misc.dup_stream(current)
          Log.medium "Duplicating file #{stream_key} #{current.inspect} => #{Misc.fingerprint(new)}"
          new
        end
      else
        new = Misc.dup_stream(current)
        Log.medium "Duplicating stream #{stream_key} #{ Misc.fingerprint(stream) } => #{Misc.fingerprint(new)}"
        new
      end
    end
  when TSV::Dumper#, TSV::Parser
    stream = stream.stream
    return stream if stream.closed?

    STREAM_CACHE_MUTEX.synchronize do
      if STREAM_CACHE[stream].nil?
        Log.high "Not duplicating dumper #{ stream.inspect }"
        STREAM_CACHE[stream] = stream
      else
        new = Misc.dup_stream(STREAM_CACHE[stream])
        Log.high "Duplicating dumper #{ stream.inspect } into #{new.inspect}"
        new
      end
    end
  else
    stream
  end
end

.files_dir(path) ⇒ Object



41
42
43
# File 'lib/rbbt/workflow/accessor.rb', line 41

def self.files_dir(path)
  path.nil? ? nil : path + '.files'
end

.info_file(path) ⇒ Object



45
46
47
# File 'lib/rbbt/workflow/accessor.rb', line 45

def self.info_file(path)
  path.nil? ? nil : path + '.info'
end

.job_name_for_info_file(info_file, extension = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/rbbt/workflow/accessor.rb', line 64

def self.job_name_for_info_file(info_file, extension = nil)
  if extension and not extension.empty?
    info_file.sub(/\.#{extension}\.info$/,'')
  else
    info_file.sub(/\.info$/,'')
  end
end

.log(status, message, path, &block) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rbbt/workflow/accessor.rb', line 286

def self.log(status, message, path, &block)
  if block
    if Hash === message
      log_progress(status, message, path, &block)
    else
      log_block(status, message, path, &block)
    end
  else
    log_string(status, message, path)
  end
end

.log_block(status, message, path, &block) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rbbt/workflow/accessor.rb', line 216

def self.log_block(status, message, path, &block)
  start = Time.now
  status = status.to_s
  status_color = self.status_color status

  Log.info do 
    now = Time.now
    str = Log.color :reset
    str << "#{ Log.color status_color, status}"
    str << ": #{ message }" if message
    str << " -- #{Log.color :blue, path.to_s}" if path
    str << " #{Log.color :yellow, Process.pid}"
    str
  end
  res = yield
  eend = Time.now
  Log.info do 
    now = Time.now
    str = "#{ Log.color :cyan, status.to_s } +#{Log.color :green, "%.2f" % (eend - start)}"
    str << " -- #{Log.color :blue, path.to_s}" if path
    str << " #{Log.color :yellow, Process.pid}"
    str
  end
  res
end

.log_progress(status, options = {}, path = nil, &block) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rbbt/workflow/accessor.rb', line 257

def self.log_progress(status, options = {}, path = nil, &block)
  options = Misc.add_defaults options, :severity => Log::INFO, :file => path
  max = Misc.process_options options, :max
  Log::ProgressBar.with_bar(max, options) do |bar|
    begin
      res = yield bar
      raise KeepBar.new res if IO === res
      res
    rescue
      Log.exception $!
    end
  end
end

.log_string(status, message, path) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/rbbt/workflow/accessor.rb', line 242

def self.log_string(status, message, path)
  Log.info do 

    status = status.to_s
    status_color = self.status_color status

    str = Log.color :reset
    str << "#{ Log.color status_color, status}"
    str << ": #{ message }" if message
    str << " -- #{Log.color :blue, path.to_s}" if path
    str << " #{Log.color :yellow, Process.pid}"
    str
  end
end

.pid_file(path) ⇒ Object



49
50
51
# File 'lib/rbbt/workflow/accessor.rb', line 49

def self.pid_file(path)
  path.nil? ? nil : path + '.pid'
end

.prepare_for_execution(job) ⇒ Object

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rbbt/workflow/step/dependencies.rb', line 75

def self.prepare_for_execution(job)
  return if job.done? && ! job.dirty?

  status = job.status.to_s

  if defined?(WorkflowRESTClient) && WorkflowRESTClient::RemoteStep === job 
    return unless (status == 'done' or status == 'error' or status == 'aborted')
  else
    return if status == 'streaming' and job.running?
  end

  if (status == 'error' || job.aborted?) && job.recoverable_error?
    job.clean 
  end

  job.dup_inputs unless status == 'done' or job.started?

  raise DependencyError, job if job.error?
end

.purge_stream_cacheObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rbbt/workflow/step/dependencies.rb', line 6

def self.purge_stream_cache
  Log.debug "Purging dup. stream cache"
  STREAM_CACHE_MUTEX.synchronize do
    #STREAM_CACHE.collect{|k,s| 
    #  Thread.new do
    #    Misc.consume_stream s
    #  end
    #}
    STREAM_CACHE.clear
  end
end

.status_color(status) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rbbt/workflow/accessor.rb', line 202

def self.status_color(status)
  status = status.split(">").last
  case status
  when "starting"
    :yellow
  when "error", "aborted"
    :red
  when "done"
    :green
  else
    :cyan
  end
end

.step_info(path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/rbbt/workflow/accessor.rb', line 53

def self.step_info(path)
  begin
    Open.open(info_file(path)) do |f|
      INFO_SERIALIAZER.load(f)
    end
  rescue Exception
    Log.exception $!
    {}
  end
end

.wait_for_jobs(jobs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbbt/workflow/accessor.rb', line 17

def self.wait_for_jobs(jobs)
  jobs = [jobs] if Step === jobs
  begin
    threads = []

    threads = jobs.collect do |j| 
      Thread.new do
        begin
          j.join unless j.done?
        rescue Exception
          Log.error "Exception waiting for job: #{Log.color :blue, j.path}"
          raise $!
        end
      end
    end

    threads.each{|t| t.join }
  rescue Exception
    threads.each{|t| t.exit }
    jobs.each do |j| j.abort end
    raise $!
  end
end

Instance Method Details

#_abortObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/rbbt/workflow/step/run.rb', line 388

def _abort
  return if @aborted
  @aborted = true
  Log.medium{"#{Log.color :red, "Aborting"} #{Log.color :blue, path}"}
  begin
    return if done?
    stop_dependencies
    abort_stream
    abort_pid if running?
  rescue Aborted, Interrupt
    Log.medium{"#{Log.color :red, "Aborting ABORTED RETRY"} #{Log.color :blue, path}"}
    retry
  rescue Exception
    Log.exception $!
    retry
  ensure
    _clean_finished
  end
end

#_clean_finishedObject



377
378
379
380
381
382
383
384
385
386
# File 'lib/rbbt/workflow/step/run.rb', line 377

def _clean_finished
  if Open.exists? path and not status == :done
    Log.warn "Aborted job had finished. Removing result -- #{ path }"
    begin
      Open.rm path
    rescue Exception
      Log.warn "Exception removing result of aborted job: #{$!.message}"
    end
  end
end

#_execObject



54
55
56
57
58
# File 'lib/rbbt/workflow/step/run.rb', line 54

def _exec
  resolve_input_steps
  @exec = true if @exec.nil?
  @task.exec_in((bindings ? bindings : self), *@inputs)
end

#abortObject



408
409
410
411
412
413
# File 'lib/rbbt/workflow/step/run.rb', line 408

def abort
  return if done? and status == :done
  _abort
  log(:aborted, "Job aborted") unless aborted? or error?
  self
end

#abort_pidObject



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/rbbt/workflow/step/run.rb', line 339

def abort_pid
  @pid ||= info[:pid]

  case @pid
  when nil
    Log.medium "Could not abort #{path}: no pid"
    false
  when Process.pid
    Log.medium "Could not abort #{path}: same process"
    false
  else
    Log.medium "Aborting pid #{path}: #{ @pid }"
    begin
      Process.kill("INT", @pid)
      Process.waitpid @pid
    rescue Exception
      Log.debug("Aborted job #{@pid} was not killed: #{$!.message}")
    end
    Log.medium "Aborted pid #{path}: #{ @pid }"
    true
  end
end

#abort_streamObject



362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/rbbt/workflow/step/run.rb', line 362

def abort_stream
  stream = @result if IO === @result
  @saved_stream = nil
  if stream and stream.respond_to? :abort and not stream.aborted?
    begin
      Log.medium "Aborting job stream #{stream.inspect} -- #{Log.color :blue, path}"
      stream.abort 
    rescue Aborted, Interrupt
      Log.medium "Aborting job stream #{stream.inspect} ABORTED RETRY -- #{Log.color :blue, path}"
      Log.exception $!
      retry
    end
  end
end

#aborted?Boolean

Returns:

  • (Boolean)


389
390
391
# File 'lib/rbbt/workflow/accessor.rb', line 389

def aborted?
  status == :aborted || nopid?
end

#checksObject



69
70
71
# File 'lib/rbbt/workflow/step/run.rb', line 69

def checks
  rec_dependencies.collect{|dependency| (defined? WorkflowRESTClient and WorkflowRESTClient::RemoteStep === dependency) ? nil : dependency.path }.compact.uniq
end

#child(&block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rbbt/workflow/step.rb', line 171

def child(&block)
  child_pid = Process.fork &block
  children_pids = info[:children_pids]
  if children_pids.nil?
    children_pids = [child_pid]
  else
    children_pids << child_pid
  end
  #Process.detach(child_pid)
  set_info :children_pids, children_pids
  child_pid
end

#cleanObject



223
224
225
226
227
228
# File 'lib/rbbt/workflow/step.rb', line 223

def clean
  Log.medium "Cleaning step: #{path}"
  abort if not done? and running?
  Step.clean(path)
  self
end

#dirty?Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/rbbt/workflow/accessor.rb', line 352

def dirty?
  rec_dependencies.collect{|dependency| dependency.path }.uniq.reject{|path| not Path === path or path.exists?}.any?
end

#done?Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/rbbt/workflow/accessor.rb', line 356

def done?
  path and File.exist? path
end

#dup_inputsObject



65
66
67
68
69
70
71
72
73
# File 'lib/rbbt/workflow/step/dependencies.rb', line 65

def dup_inputs
  return if @dupped or ENV["RBBT_NO_STREAM"] == 'true'
  Log.low "Dupping inputs for #{path}"
  dupped_inputs = @inputs.collect do |input|
    Step.dup_stream input
  end
  @inputs.replace dupped_inputs
  @dupped = true
end

#error?Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/rbbt/workflow/accessor.rb', line 380

def error?
  status == :error
end

#exception(ex, msg = nil) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rbbt/workflow/accessor.rb', line 306

def exception(ex, msg = nil)
  ex_class = ex.class.to_s
  set_info :backtrace, ex.backtrace
  set_info :exception, {:class => ex_class, :message => ex.message, :backtrace => ex.backtrace}
  if msg.nil?
    log :error, "#{ex_class} -- #{ex.message}"
  else
    log :error, "#{msg} -- #{ex.message}"
  end
  self._abort
end

#execute_and_dup(step, dep_step, log = true) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rbbt/workflow/step/dependencies.rb', line 172

def execute_and_dup(step, dep_step, log = true)
  dup = step.result.nil?
  execute_dependency(step, log)
  if dup and step.streaming? and not step.result.nil?
    if dep_step[step.path] and dep_step[step.path].length > 1
      stream = step.result
      other_steps = dep_step[step.path]
      return unless other_steps.length > 1
      log_dependency_exec(step, "duplicating #{other_steps.length}") 
      copies = Misc.tee_stream_thread_multiple(stream, other_steps.length)
      other_steps.zip(copies).each do |other,dupped_stream|
        stream.annotate(dupped_stream) if stream.respond_to?(:annotate)
        other.instance_variable_set("@result", dupped_stream)
      end
    end
  end
end

#execute_dependency(dependency, log = true) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rbbt/workflow/step/dependencies.rb', line 110

def execute_dependency(dependency, log = true)
  task_name = self.task_name
  begin

    dependency.resolve_input_steps

    if dependency.done?
      dependency.inputs.each do |v|
        Misc.consume_stream(v) if IO === v
        Misc.consume_stream(TSV.get_stream v) if Step === v and not v.done?  and  v.streaming?
      end
      log_dependency_exec(dependency, :done) if log
      return
    end

    if not dependency.started?
      log_dependency_exec(dependency, :starting)
      dependency.run(true)
      raise TryAgain
    end

    dependency.grace

    if dependency.aborted?
      log_dependency_exec(dependency, "aborted (clean)")
      dependency.clean
      raise TryAgain
    end

    if dependency.error?
      log_dependency_exec(dependency, :error)
      raise DependencyError, [dependency.path, dependency.messages.last] * ": " if dependency.error?
    end

    if dependency.streaming?
      log_dependency_exec(dependency, :streaming) if log
      return
    end

    begin
      log_dependency_exec(dependency, :joining)
      dependency.join
      raise TryAgain unless dependency.done?
    rescue Aborted
      raise TryAgain
    end

  rescue TryAgain
    retry
  rescue Aborted, Interrupt
    Log.error "Aborted dep. #{Log.color :red, dependency.task_name.to_s}"
    raise $!
  rescue Interrupt
    Log.error "Interrupted while in dep. #{Log.color :red, dependency.task_name.to_s}"
    raise $!
  rescue Exception
    Log.error "Exception in dep. #{ Log.color :red, dependency.task_name.to_s } -- #{$!.message}"
    Log.exception $!
    raise $!
  end
end

#file(name) ⇒ Object



406
407
408
# File 'lib/rbbt/workflow/accessor.rb', line 406

def file(name)
  Path.setup(File.join(files_dir, name.to_s))
end

#filesObject



399
400
401
402
403
404
# File 'lib/rbbt/workflow/accessor.rb', line 399

def files
  files = Dir.glob(File.join(files_dir, '**', '*')).reject{|path| File.directory? path}.collect do |path| 
    Misc.path_relative_to(files_dir, path) 
  end
  files
end

#files_dirObject

{{{ INFO



395
396
397
# File 'lib/rbbt/workflow/accessor.rb', line 395

def files_dir
  @files_dir ||= Step.files_dir path
end

#fork(no_load = false, semaphore = nil) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/rbbt/workflow/step/run.rb', line 270

def fork(no_load = false, semaphore = nil)
  raise "Can not fork: Step is waiting for proces #{@pid} to finish" if not @pid.nil? and not Process.pid == @pid and Misc.pid_exists?(@pid) and not done? and info[:forked]
  sout, sin = Misc.pipe if no_load == :stream
  @pid = Process.fork do
    sout.close if sout
    Misc.pre_fork
    begin
      RbbtSemaphore.wait_semaphore(semaphore) if semaphore
      FileUtils.mkdir_p File.dirname(path) unless File.exist? File.dirname(path)
      begin
        @forked = true
        res = run no_load
        set_info :forked, true
        if sin
          io = TSV.get_stream res
          if io.respond_to? :setup
            io.setup(sin) 
            sin.pair = io
            io.pair = sin
          end
          begin
            Misc.consume_stream(io, false, sin)
          rescue 
            Log.warn "Could not consume stream (#{io.closed? ? 'closed' : 'open'}) into pipe for forked job: #{self.path}"
            Misc.consume_stream(io) unless io.closed?
          end
        end
      rescue Aborted, Interrupt
        Log.debug{"Forked process aborted: #{path}"}
        log :aborted, "Job aborted (#{Process.pid})"
        raise $!
      rescue Exception
        Log.debug("Exception '#{$!.message}' caught on forked process: #{path}")
        raise $!
      ensure
        join_stream
      end

      begin
        children_pids = info[:children_pids]
        if children_pids
          children_pids.each do |pid|
            if Misc.pid_exists? pid
              begin
                Process.waitpid pid
              rescue Errno::ECHILD
                Log.low "Waiting on #{ pid } failed: #{$!.message}"
              end
            end
          end
          set_info :children_done, Time.now
        end
      rescue Exception
        Log.debug("Exception waiting for children: #{$!.message}")
        RbbtSemaphore.post_semaphore(semaphore) if semaphore
        Kernel.exit! -1
      end
      set_info :pid, nil
    ensure
      RbbtSemaphore.post_semaphore(semaphore) if semaphore
      Kernel.exit! 0
    end
  end
  sin.close if sin
  @result = sout if sout 
  Process.detach(@pid)
  self
end

#get_exceptionObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/rbbt/workflow/accessor.rb', line 318

def get_exception
  if info[:exception].nil?
    raise Aborted if aborted?
    raise messages.last if error?
    return false 
  else
    ex_class, ex_message, ex_backtrace = info[:exception].values_at :class, :message, :backtrace
    begin
      klass = Kernel.const_get(ex_class)
      ex = klass.new ex_message
      #ex.set_backtrace ex_backtrace
      ex
    rescue
      Log.exception $!
      raise ex_message
    end
  end
end

#get_streamObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rbbt/workflow/step/run.rb', line 7

def get_stream
  @mutex.synchronize do
    Log.low "Getting stream from #{path} #{!@saved_stream} [#{object_id}-#{Misc.fingerprint(@result)}]"
    begin
      return nil if @saved_stream
      if IO === @result 
        @saved_stream = @result 
      else 
        nil
      end
    end
  end
end

#graceObject



436
437
438
439
440
441
# File 'lib/rbbt/workflow/step/run.rb', line 436

def grace
  until done? or result or error? or aborted? or streaming? 
    sleep 1 
  end
  self
end

#info(check_lock = true) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbbt/workflow/accessor.rb', line 102

def info(check_lock = true)
  return {} if info_file.nil? or not Open.exists? info_file
  begin
    Misc.insist do
      begin
        return @info_cache if @info_cache and @info_cache_time and File.ctime(info_file) < @info_cache_time
      rescue Exception
        raise $!
      end

      begin
        @info_cache = Misc.insist(3, 1.6, info_file) do
          Misc.insist(2, 1, info_file) do
            Misc.insist(3, 0.2, info_file) do
              raise TryAgain, "Info locked" if check_lock and info_lock.locked?
              info_lock.lock if check_lock and false
              begin
                Open.open(info_file) do |file|
                  INFO_SERIALIAZER.load(file) #|| {}
                end
              ensure
                info_lock.unlock if check_lock and false
              end
            end
          end
        end
        @info_cache_time = Time.now
        @info_cache
      end
    end
  rescue Exception
    Log.debug{"Error loading info file: " + info_file}
    Log.exception $!
    Open.rm info_file
    Misc.sensiblewrite(info_file, INFO_SERIALIAZER.dump({:status => :error, :messages => ["Info file lost"]}))
    raise $!
  end
end

#info_fileObject

{{{ INFO



86
87
88
# File 'lib/rbbt/workflow/accessor.rb', line 86

def info_file
  @info_file ||= Step.info_file(path)
end

#info_lockObject



94
95
96
97
98
99
100
# File 'lib/rbbt/workflow/accessor.rb', line 94

def info_lock
  @info_lock = begin
                 path = Persist.persistence_path(info_file + '.lock', {:dir => Step.lock_dir})
                 Lockfile.new path, :refresh => false, :dont_use_lock_id => true
               end if @info_lock.nil?
  @info_lock
end

#init_infoObject



141
142
143
144
145
146
147
148
149
# File 'lib/rbbt/workflow/accessor.rb', line 141

def init_info
  return nil if @exec or info_file.nil?
  Open.lock(info_file, :lock => info_lock) do
    i = {:status => :init, :pid => Process.pid}
    @info_cache = i
    Misc.sensiblewrite(info_file, INFO_SERIALIAZER.dump(i), :force => true, :lock => false)
    @info_cache_time = Time.now
  end
end

#joinObject



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/rbbt/workflow/step/run.rb', line 443

def join

  grace

  if streaming?
    join_stream 
  end

  return self if not Open.exists? info_file

  return self if info[:joined]

  pid = @pid 

  Misc.insist [0.1, 0.2, 0.5, 1] do
    pid ||= info[:pid]
  end

  begin

    if pid.nil? or Process.pid == pid
      dependencies.each{|dep| dep.join }
    else
      begin
        Log.debug{"Waiting for pid: #{pid}"}
        Process.waitpid pid 
      rescue Errno::ECHILD
        Log.debug{"Process #{ pid } already finished: #{ path }"}
      end if Misc.pid_exists? pid
      pid = nil
      dependencies.each{|dep| dep.join }
    end

    sleep 1 until path.exists? or error? or aborted?

    self
  ensure
    set_info :joined, true
    @result = nil
  end
end

#join_streamObject



415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/rbbt/workflow/step/run.rb', line 415

def join_stream
  stream = get_stream if @result
  @result = nil
  if stream
    begin
      Misc.consume_stream stream 
      stream.join if stream.respond_to? :join
    rescue Exception
      stream.abort $!
      self._abort
    end
  end
end

#kill_childrenObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rbbt/workflow/step/run.rb', line 73

def kill_children
  begin
    children_pids = info[:children_pids]
    if children_pids and children_pids.any?
      Log.medium("Killing children: #{ children_pids * ", " }")
      children_pids.each do |pid|
        Log.medium("Killing child #{ pid }")
        begin
          Process.kill "INT", pid
        rescue Exception
          Log.medium("Exception killing child #{ pid }: #{$!.message}")
        end
      end
    end
  rescue
    Log.medium("Exception finding children")
  end
end

#loadObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rbbt/workflow/step.rb', line 185

def load
  res = if @result and not @path == @result
          res = @result
        else
          join if not done?
          @path.exists? ? Persist.load_file(@path, @task.result_type) : exec
        end

  if @task.result_description
    entity_info = info.dup
    entity_info.merge! info[:inputs] if info[:inputs]
    res = prepare_result res, @task.result_description, entity_info 
  end

  res
end

#load_file(name, type = nil, options = {}) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/rbbt/workflow/accessor.rb', line 426

def load_file(name, type = nil, options = {})
  if type.nil? and name =~ /.*\.(\w+)$/
    extension = name.match(/.*\.(\w+)$/)[1]
    case extension
    when "tc"
      type = :tc
    when "tsv"
      type = :tsv
    when "list", "ary", "array"
      type = :array
    when "yaml"
      type = :yaml
    when "marshal"
      type = :marshal
    else
      type = :other
    end
  else
    type ||= :other
  end

  case type.to_sym
  when :tc
    Persist.open_tokyocabinet(file(name), false)
  when :tsv
    TSV.open Open.open(file(name)), options
  when :array
    #Open.read(file(name)).split /\n|,\s*/
    Open.read(file(name)).split "\n"
  when :yaml
    YAML.load(Open.open(file(name)))
  when :marshal
    Marshal.load(Open.open(file(name)))
  else
    Open.read(file(name))
  end
end

#log(status, message = nil, &block) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/rbbt/workflow/accessor.rb', line 298

def log(status, message = nil, &block)
  self.status = status
  if message
    self.message Log.uncolor(message)
  end
  Step.log(status, message, path, &block)
end

#log_dependency_exec(dependency, action) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbbt/workflow/step/dependencies.rb', line 95

def log_dependency_exec(dependency, action)
  task_name = self.task_name

  str = Log.color(:reset, "")
  str << Log.color(:yellow, task_name.to_s || "") 
  str << " "
  str << Log.color(:magenta, action.to_s)
  str << " "
  str << Log.color(:yellow, dependency.task_name.to_s || "")
  str << " -- "
  str << "#{Log.color :blue, dependency.path}"

  Log.info str
end

#log_progress(status, options = {}, &block) ⇒ Object



271
272
273
# File 'lib/rbbt/workflow/accessor.rb', line 271

def log_progress(status, options = {}, &block)
  Step.log_progress(status, options, file(:progress), &block)
end

#merge_info(hash) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rbbt/workflow/accessor.rb', line 164

def merge_info(hash)
  return nil if @exec or info_file.nil?
  value = Annotated.purge value if defined? Annotated
  Open.lock(info_file, :lock => info_lock) do
    i = info(false)
    i.merge! hash
    @info_cache = i
    Misc.sensiblewrite(info_file, INFO_SERIALIAZER.dump(i), :force => true, :lock => false)
    @info_cache_time = Time.now
    value
  end
end

#message(message) ⇒ Object



198
199
200
# File 'lib/rbbt/workflow/accessor.rb', line 198

def message(message)
  set_info(:messages, (messages || []) << message)
end

#messagesObject



190
191
192
193
194
195
196
# File 'lib/rbbt/workflow/accessor.rb', line 190

def messages
  if messages = info[:messages]
    messages
  else
    set_info(:messages, []) if self.respond_to?(:set_info)
  end
end

#nameObject



72
73
74
# File 'lib/rbbt/workflow/accessor.rb', line 72

def name
  path.sub(/.*\/#{Regexp.quote task.name.to_s}\/(.*)/, '\1')
end

#nopid?Boolean

Returns:

  • (Boolean)


384
385
386
387
# File 'lib/rbbt/workflow/accessor.rb', line 384

def nopid?
  pid = info[:pid]
  pid.nil? && ! (status.nil? || status.nil? || status == :aborted || status == :done || status == :error)
end

#pid_fileObject



90
91
92
# File 'lib/rbbt/workflow/accessor.rb', line 90

def pid_file
  @pid_file ||= Step.pid_file(path)
end

#prepare_result(value, description = nil, entity_info = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbbt/workflow/step.rb', line 97

def prepare_result(value, description = nil, entity_info = nil)
  res = case 
  when IO === value
    begin
      res = case @task.result_type
            when :array
              array = []
              while line = value.gets
                array << line.strip
              end
              array
            when :tsv
              begin
                TSV.open(value)
              rescue IOError
                TSV.setup({})
              end
            else
              value.read
            end
      value.join if value.respond_to? :join
      res
    rescue Exception
      value.abort if value.respond_to? :abort
      self.abort
      raise $!
    end
  when (not defined? Entity or description.nil? or not Entity.formats.include? description)
    value
  when (Annotated === value and info.empty?)
    value
  when Annotated === value
    annotations = value.annotations
    entity_info ||= begin 
                      entity_info = info.dup
                      entity_info.merge! info[:inputs] if info[:inputs]
                      entity_info
                    end
    entity_info.each do |k,v|
      value.send("#{h}=", v) if annotations.include? k
    end
                      
    value
  else
    entity_info ||= begin 
                      entity_info = info.dup
                      entity_info.merge! info[:inputs] if info[:inputs]
                      entity_info
                    end
    Entity.formats[description].setup(value, entity_info.merge(:format => description))
  end

  if Annotated === res
    dep_hash = nil
    res.annotations.each do |a|
      a = a.to_s
      varname = "@" + a
      next unless res.instance_variable_get(varname).nil? 

      dep_hash ||= begin
                     h = {}
                     rec_dependencies.each{|dep| h[dep.task_name.to_s] ||= dep }
                     h
                   end
      dep = dep_hash[a]
      next if dep.nil?
      res.send(a.to_s+"=", dep.load)
    end 
  end

  res
end

#produce(force = false, dofork = false) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rbbt/workflow/step/run.rb', line 243

def produce(force=false, dofork=false)
  return self if done? and not dirty?

  if error? or aborted? or stalled?
    abort if stalled?
    if force or aborted? or recoverable_error?
      clean
    else
      raise "Error in job: #{status} - #{self.path}"
    end
  end

  clean if dirty? or (not running? and not done?)

  if dofork
    fork(true) unless started?

    join unless done?
  else
    run(true) unless started?

    join unless done?
  end

  self
end

#progress_bar(msg = "Progress", options = nil) ⇒ Object



275
276
277
278
279
280
281
282
283
284
# File 'lib/rbbt/workflow/accessor.rb', line 275

def progress_bar(msg = "Progress", options = nil)
  if Hash === msg and options.nil?
    options = msg
    msg = nil
  end
  options = {} if options.nil?

  max = options[:max]
  Log::ProgressBar.new_bar(max, {:desc => msg, :file => file(:progress)}.merge(options))
end

#provenanceObject



464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/rbbt/workflow/accessor.rb', line 464

def provenance
  provenance = {}
  dependencies.each do |dep|
    next unless dep.path.exists?
    if File.exist? dep.info_file
      provenance[dep.path] = dep.provenance if File.exist? dep.path
    else
      provenance[dep.path] = nil
    end
  end
  {:inputs => info[:inputs], :provenance => provenance}
end

#provenance_pathsObject



477
478
479
480
481
482
483
# File 'lib/rbbt/workflow/accessor.rb', line 477

def provenance_paths
  provenance = {}
  dependencies.each do |dep|
    provenance[dep.path] = dep.provenance_paths if File.exist? dep.path
  end
  provenance
end

#rec_dependenciesObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rbbt/workflow/step.rb', line 230

def rec_dependencies

  # A step result with no info_file means that it was manually
  # placed. In that case, do not consider its dependencies
  return [] if not (defined? WorkflowRESTClient and  WorkflowRESTClient::RemoteStep === self) and Open.exists?(self.path.to_s) and not Open.exists? self.info_file

  return [] if dependencies.nil? or dependencies.empty?

  new_dependencies = []
  dependencies.each{|step| 
    r = step.rec_dependencies
    new_dependencies.concat r
    new_dependencies << step
  }
  new_dependencies.uniq
end

#recoverable_error?Boolean

Returns:

  • (Boolean)


337
338
339
340
341
342
343
344
345
346
# File 'lib/rbbt/workflow/accessor.rb', line 337

def recoverable_error?
  return true if aborted?
  return false unless error?
  begin
    klass = Kernel.const_get(info[:exception][:class])
    not RbbtException === klass
  rescue Exception
    false
  end
end

#recursive_cleanObject



247
248
249
250
251
252
253
# File 'lib/rbbt/workflow/step.rb', line 247

def recursive_clean
  dependencies.each do |step| 
    step.recursive_clean 
  end
  clean if Open.exists?(self.info_file)
  self
end

#relay_log(step) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbbt/workflow/step.rb', line 80

def relay_log(step)
  return self unless Task === self.task and not self.task.name.nil?
  if not self.respond_to? :original_log
    class << self
      attr_accessor :relay_step
      alias original_log log 
      def log(status, message = nil)
        self.status = status
        message Log.uncolor message
        relay_step.log([task.name.to_s, status.to_s] * ">", message.nil? ? nil : message ) unless (relay_step.done? or relay_step.error? or relay_step.aborted?)
      end
    end
  end
  @relay_step = step
  self
end

#resolve_input_stepsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rbbt/workflow/step/run.rb', line 21

def resolve_input_steps
  step = false
  pos = 0
  new_inputs = @inputs.collect do |i| 
    begin
      if Step === i
        step = true
        if i.done?
          if (task.input_options[task.inputs[pos]] || {})[:stream]
            TSV.get_stream i
          else
            i.load
          end
        elsif i.streaming?
          TSV.get_stream i
        else
          i.join
          if (task.input_options[task.inputs[pos]] || {})[:stream]
            TSV.get_stream i
          else
            i.load
          end
        end
      else
        i
      end
    ensure
      pos += 1
    end
  end
  @inputs.replace new_inputs if step
end

#run(no_load = false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rbbt/workflow/step/run.rb', line 92

def run(no_load = false)
  result = nil

  begin
    @mutex.synchronize do
      no_load = :stream if no_load
      result = Persist.persist "Job", @task.result_type, :file => path, :check => checks, :no_load => no_load do 
        if Step === Step.log_relay_step and not self == Step.log_relay_step
          relay_log(Step.log_relay_step) unless self.respond_to? :relay_step and self.relay_step
        end

        @exec = false
        Open.write(pid_file, Process.pid.to_s)
        init_info

        log :setup, "#{Log.color :green, "Setup"} step #{Log.color :yellow, task.name.to_s || ""}"

        merge_info({
          :issued => (issue_time = Time.now),
          :name => name,
          :clean_name => clean_name,
        })

        set_info :dependencies, dependencies.collect{|dep| [dep.task_name, dep.name, dep.path]}

        begin
          run_dependencies
        rescue Exception
          FileUtils.rm pid_file if File.exist?(pid_file)
          stop_dependencies
          raise $!
        end

        set_info :inputs, Misc.remove_long_items(Misc.zip2hash(task.inputs, @inputs)) unless task.inputs.nil?

        set_info :started, (start_time = Time.now)
        log :started, "Starting step #{Log.color :yellow, task.name.to_s || ""}"

        begin
          result = _exec
        rescue Aborted, Interrupt
          log(:aborted, "Aborted")
          raise $!
        rescue Exception
          backtrace = $!.backtrace

          # HACK: This fixes an strange behaviour in 1.9.3 where some
          # backtrace strings are coded in ASCII-8BIT
          backtrace.each{|l| l.force_encoding("UTF-8")} if String.instance_methods.include? :force_encoding
          set_info :backtrace, backtrace 
          log(:error, "#{$!.class}: #{$!.message}")
          stop_dependencies
          raise $!
        end

        if not no_load or ENV["RBBT_NO_STREAM"] == "true" 
          result = prepare_result result, @task.description, info if IO === result 
          result = prepare_result result.stream, @task.description, info if TSV::Dumper === result 
        end

        stream = case result
                 when IO
                   result
                 when TSV::Dumper
                   result.stream
                 end

        if stream
          log :streaming, "Streaming step #{Log.color :yellow, task.name.to_s || ""}"

          callback = Proc.new do
            if AbortedStream === stream
              if stream.exception
                raise stream.exception 
              else
                raise Aborted
              end
            end
            begin
              if status != :done
                Misc.insist do
                  set_info :done, (done_time = Time.now)
                  set_info :total_time_elapsed, (total_time_elapsed = done_time - issue_time)
                  set_info :time_elapsed, (time_elapsed = done_time - start_time)
                  log :done, "Completed step #{Log.color :yellow, task.name.to_s || ""} in #{time_elapsed.to_i}+#{(total_time_elapsed - time_elapsed).to_i} sec."
                end
              end
            rescue
              Log.exception $!
            ensure
              Step.purge_stream_cache
              FileUtils.rm pid_file if File.exist?(pid_file)
            end
          end

          abort_callback = Proc.new do |exception|
            begin
              if exception
                self.exception exception
              else
                log :aborted, "#{Log.color :red, "Aborted"} step #{Log.color :yellow, task.name.to_s || ""}" if status == :streaming
              end
              _clean_finished
            rescue
              Log.exception $!
              stop_dependencies
              FileUtils.rm pid_file if File.exist?(pid_file)
            end
          end

          ConcurrentStream.setup stream, :callback => callback, :abort_callback => abort_callback

          if AbortedStream === stream 
            exception = stream.exception || Aborted
            self.exception exception
            _clean_finished
            raise exception
          end
        else
          set_info :done, (done_time = Time.now)
          set_info :total_time_elapsed, (total_time_elapsed = done_time - issue_time)
          set_info :time_elapsed, (time_elapsed = done_time - start_time)
          log :done, "Completed step #{Log.color :yellow, task.name.to_s || ""} in #{time_elapsed.to_i}+#{(total_time_elapsed - time_elapsed).to_i} sec."
          Step.purge_stream_cache
          FileUtils.rm pid_file if File.exist?(pid_file)
        end

        set_info :dependencies, dependencies.collect{|dep| [dep.task_name, dep.name, dep.path]}

        result
      end

      if no_load
        @result ||= result
        self
      else
        Step.purge_stream_cache
        @result = prepare_result result, @task.result_description
      end
    end
  rescue Aborted, Interrupt
    abort
    stop_dependencies
    raise $!
  rescue Exception
    exception $!
    stop_dependencies
    raise $!
  end
end

#run_compute_dependencies(type, list, dep_step = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rbbt/workflow/step/dependencies.rb', line 190

def run_compute_dependencies(type, list, dep_step = {})
  if Array === type
    type, *rest = type
  end

  case type
  when :produce, :no_dup
    list.each do |step|
      Misc.insist do
        begin
          step.produce
        rescue Exception
          step.abort
          raise $!
        end
      end
      nil
    end
  when :bootstrap
    cpus = rest.nil? ? nil : rest.first 
    cpus = 5 if cpus.nil?
    cpus = list.length / 2 if cpus > list.length / 2

    Misc.bootstrap(list, cpus, :bar => "Bootstrapping dependencies for #{path}", :respawn => :always) do |dep|
      Misc.insist do
        begin
          dep.produce 
          Log.warn "Error in bootstrap dependency #{dep.path}: #{dep.messages.last}" if dep.error? or dep.aborted?
        rescue Aborted
          dep.abort
          Log.warn "Aborted bootstrap dependency #{dep.path}: #{dep.messages.last}" if dep.error? or dep.aborted?
          raise $!
        rescue Exception
          dep.exception $!
          raise StopInsist.new($!)
        end
      end
      nil
    end
  else
    list.each do |step|
      execute_and_dup(step, dep_step, false)
    end
  end
end

#run_dependenciesObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rbbt/workflow/step/dependencies.rb', line 236

def run_dependencies
  dep_step = {}

  rec_dependencies = self.rec_dependencies

  return if rec_dependencies.empty?

  all_deps = rec_dependencies + [self]

  dependencies.each do |dep|
    next unless ComputeDependency === dep
    if dep.compute == :produce
      dep.produce 
    end
  end

  compute_deps = rec_dependencies.collect do |dep|
    next unless ComputeDependency === dep
    dep.rec_dependencies
  end.compact.flatten.uniq

  seen_paths = Set.new
  all_deps.uniq.each do |step|
    next if seen_paths.include? step.path
    seen_paths << step.path
    Step.prepare_for_execution(step) unless step == self
    next unless step.dependencies and step.dependencies.any?
    step.dependencies.each do |step_dep|
      next if step_dep.done? or step_dep.running? or (ComputeDependency === step_dep and step_dep.compute == :nodup)
      dep_step[step_dep.path] ||= []
      dep_step[step_dep.path] << step_dep
    end
  end
  
  self.dup_inputs

  required_dep_paths = []
  dep_step.each do |path,list|
    required_dep_paths << path if list.length > 1
  end

  required_dep_paths.concat dependencies.collect{|dep| dep.path }


  log :dependencies, "Dependencies for step #{Log.color :yellow, task.name.to_s || ""}"

  pre_deps = []
  compute_pre_deps = {}
  last_deps = []
  compute_last_deps = {}
  seen_paths = Set.new
  rec_dependencies.uniq.each do |step| 
    next if seen_paths.include? step.path
    seen_paths << step.path
    next unless required_dep_paths.include? step.path
    if dependencies.include?(step) and step.inputs.flatten.select{|i| Step === i}.any?
      if ComputeDependency === step
        compute_last_deps[step.compute] ||= []
        compute_last_deps[step.compute] << step
      else
        last_deps << step
      end
    else
      if ComputeDependency === step
        compute_pre_deps[step.compute] ||= []
        compute_pre_deps[step.compute] << step
      else
        pre_deps << step #if dependencies.include?(step)
      end
    end
  end

  Log.medium "Processing pre dependencies: #{Misc.fingerprint(pre_deps)} - #{Log.color :blue, self.path}" if pre_deps.any?
  pre_deps.each do |step|
    next if compute_deps.include? step
    execute_and_dup(step, dep_step, false)
  end

  Log.medium "Computing pre dependencies: #{Misc.fingerprint(compute_pre_deps)} - #{Log.color :blue, self.path}" if compute_pre_deps.any?
  compute_pre_deps.each do |type,list|
    run_compute_dependencies(type, list, dep_step)
  end

  Log.medium "Processing last dependencies: #{Misc.fingerprint(last_deps)} - #{Log.color :blue, self.path}" if last_deps.any?
  last_deps.each do |step|
    next if compute_deps.include? step
    execute_and_dup(step, dep_step)
  end

  Log.medium "Computing last dependencies: #{Misc.fingerprint(compute_last_deps)} - #{Log.color :blue, self.path}" if compute_last_deps.any?
  compute_last_deps.each do |type,list|
    run_compute_dependencies(type, list, dep_step)
  end

end

#running?Boolean

Returns:

  • (Boolean)


365
366
367
368
369
370
371
372
373
374
# File 'lib/rbbt/workflow/accessor.rb', line 365

def running?
  pid = info[:pid]
  return nil if pid.nil?

  if Misc.pid_exists?(pid) 
    pid
  else
    false
  end
end

#save_file(name, content) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/rbbt/workflow/accessor.rb', line 410

def save_file(name, content)
  content = case
            when String === content
              content
            when Array === content
              content * "\n"
            when TSV === content
              content.to_s
            when Hash === content
              content.collect{|*p| p * "\t"} * "\n"
            else
              content.to_s
            end
  Open.write(file(name), content)
end

#set_info(key, value) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rbbt/workflow/accessor.rb', line 151

def set_info(key, value)
  return nil if @exec or info_file.nil?
  value = Annotated.purge value if defined? Annotated
  Open.lock(info_file, :lock => info_lock) do
    i = info(false).dup
    i[key] = value 
    @info_cache = i
    Misc.sensiblewrite(info_file, INFO_SERIALIAZER.dump(i), :force => true, :lock => false)
    @info_cache_time = Time.now
    value
  end
end

#short_pathObject



76
77
78
# File 'lib/rbbt/workflow/accessor.rb', line 76

def short_path
  [task_name, name] * "/"
end

#soft_graceObject



429
430
431
432
433
434
# File 'lib/rbbt/workflow/step/run.rb', line 429

def soft_grace
  until done? or File.exist?(info_file)
    sleep 1 
  end
  self
end

#stalled?Boolean

Returns:

  • (Boolean)


376
377
378
# File 'lib/rbbt/workflow/accessor.rb', line 376

def stalled?
  started? && ! (done? || error? || aborted?) && ! running?
end

#started?Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/rbbt/workflow/accessor.rb', line 348

def started?
  Open.exists?(path) or Open.exists?(pid_file) or Open.exists?(info_file)
end

#statusObject



177
178
179
180
181
182
183
184
# File 'lib/rbbt/workflow/accessor.rb', line 177

def status
  begin
    info[:status]
  rescue Exception
    Log.error "Exception reading status: #{$!.message}" 
    :error
  end
end

#status=(status) ⇒ Object



186
187
188
# File 'lib/rbbt/workflow/accessor.rb', line 186

def status=(status)
  set_info(:status, status)
end

#step(name) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rbbt/workflow/step.rb', line 255

def step(name)
  @steps ||= {}
  @steps[name] ||= begin
                     deps = rec_dependencies.select{|step| 
                       step.task_name.to_sym == name.to_sym
                     }
                     raise "Dependency step not found: #{ name }" if deps.empty?
                     if (deps & self.dependencies).any?
                       (deps & self.dependencies).first
                     else
                       deps.first
                     end
                   end
end

#stop_dependenciesObject



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rbbt/workflow/step/dependencies.rb', line 332

def stop_dependencies
  return if dependencies.nil?
  dependencies.each do |dep|
    begin
      next if dep.done? or dep.aborted?
    rescue
    end
    dep.abort
  end
  kill_children
end

#streaming?Boolean

Returns:

  • (Boolean)


360
361
362
# File 'lib/rbbt/workflow/accessor.rb', line 360

def streaming?
  IO === @result or @saved_stream or status == :streaming
end

#task_nameObject



64
65
66
# File 'lib/rbbt/workflow/step.rb', line 64

def task_name
  @task.name
end