Class: Miniflow::Flow

Inherits:
Object
  • Object
show all
Includes:
FileCheck
Defined in:
lib/miniflow/flow.rb

Overview

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

Instance Method Summary collapse

Methods included from FileCheck

check_file, check_file_exist, check_file_extname, mkdir_p

Constructor Details

#initializeFlow

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.

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/miniflow/flow.rb', line 17

def initialize
  raise NotImplementedError
end

Instance Method Details

#after_runObject

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



30
31
32
33
# File 'lib/miniflow/flow.rb', line 30

def after_run
  check_output_files
  show_results
end

#before_runObject

This section is mainly for file validation.



26
# File 'lib/miniflow/flow.rb', line 26

def before_run; end

#check_output_filesObject

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.



114
115
116
117
118
119
120
121
122
123
124
125
# 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

#cmdObject



21
22
23
# File 'lib/miniflow/flow.rb', line 21

def cmd
  TTYCMD
end

#dir(file_name) ⇒ Object

Returns the path in the output directory.



107
108
109
# File 'lib/miniflow/flow.rb', line 107

def dir(file_name)
  File.join(@out_dir, file_name)
end

#main_runObject

The main process. It must be implemented.

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/miniflow/flow.rb', line 36

def main_run
  raise NotImplementedError
end

#odir(file_name) ⇒ Object

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.



99
100
101
102
103
104
# 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

#runObject

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



42
43
44
45
46
47
48
49
50
51
52
53
# 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_errorObject

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



85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object

Show the banner when the workflow is finished.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_startObject

Show the banner when the workflow starts.



56
57
58
59
60
61
62
# 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