class Miniflow::Flow

Base class for all flows in Miniflow The input file must be specified as an absolute path. Each flow has one output directory

Public Class Methods

new() click to toggle source

Define the initialize method according to the following rules

  • Take file paths as arguments. The file paths can be either absolute or

relative.

  • Other arguments are keyword arguments.

# File lib/miniflow/flow.rb, line 17
def initialize
  raise NotImplementedError
end

Public Instance Methods

after_run() click to toggle source

This section is mainly used to validate the generated files, display runtime information, etc.

# File lib/miniflow/flow.rb, line 30
def after_run
  check_output_files
  show_results
end
before_run() click to toggle source

This section is mainly for file validation.

# File lib/miniflow/flow.rb, line 26
def before_run; end
check_output_files() click to toggle source

When the workflow is finished, make sure that all the registered output files are present.Currently, the error does not occur even if the output file does not exist. A warning will be shown.

# File lib/miniflow/flow.rb, line 114
def check_output_files
  return if @output_files.nil?

  flag = true
  @output_files.uniq.each do |file_path|
    unless File.exist?(file_path)
      warn "Output file not found: #{file_path}"
      flag = false
    end
  end
  puts 'All output file exist.' if flag
end
cmd() click to toggle source
# File lib/miniflow/flow.rb, line 21
def cmd
  TTYCMD
end
dir(file_name) click to toggle source

Returns the path in the output directory.

# File lib/miniflow/flow.rb, line 107
def dir(file_name)
  File.join(@out_dir, file_name)
end
main_run() click to toggle source

The main process. It must be implemented.

# File lib/miniflow/flow.rb, line 36
def main_run
  raise NotImplementedError
end
odir(file_name) click to toggle source

Returns the path in the output directory. The file path is recorded. When the workflow is finished, the output files are checked for existence in the `check_output_files` method.

# File lib/miniflow/flow.rb, line 99
def odir(file_name)
  @output_files ||= []
  file_path = File.join(@out_dir, file_name)
  @output_files << file_path
  file_path
end
run() click to toggle source

If you have a reason, you can overwrite it. Normally, you should not change it.

# File lib/miniflow/flow.rb, line 42
def run
  show_start
  before_run
  main_run
  after_run
rescue TTY::Command::ExitError => e
  show_exit_error
  raise e
rescue Errno::ENOENT => e # Maybe we should expand the scope of the error.
  show_exit_error
  raise e
end
show_exit_error() click to toggle source

Show the banner when an error occurs and the workflow is abnormally interrupted.

# File lib/miniflow/flow.rb, line 85
def show_exit_error
  @end_time = Time.now
  puts TTY::Box.error(
    "Flow   : #{self.class.name}\n" \
    "Start  : #{@start_time}\n" \
    "End    : #{@end_time}\n" \
    "Time   : #{@end_time - @start_time} sec",
    width: TTY::Screen.width, padding: 1
  )
end
show_results(generated_files: true) click to toggle source

Show the banner when the workflow is finished.

# File lib/miniflow/flow.rb, line 65
def show_results(generated_files: true)
  @end_time = Time.now
  tree = if generated_files
           [' ',
            "Generated files: #{@out_dir}",
            ' ',
            TTY::Tree.new(@out_dir).render]
         else
           ''
         end
  puts
  puts TTY::Box.frame("Start  : #{@start_time}",
                      "End    : #{@end_time}",
                      "Time   : #{@end_time - @start_time} sec",
                      *tree,
                      title: { top_left: " #{self.class.name} " },
                      width: TTY::Screen.width, padding: 1)
end
show_start() click to toggle source

Show the banner when the workflow starts.

# File lib/miniflow/flow.rb, line 56
def show_start
  @start_time = Time.now
  puts TTY::Box.frame(self.class.name,
                      "Start  : #{@start_time}",
                      width: TTY::Screen.width, padding: 1,
                      border: { left: false, right: false })
end