Class: Backup::Pipeline
- Inherits:
-
Object
- Object
- Backup::Pipeline
- Includes:
- Utilities::Helpers
- Defined in:
- lib/backup/pipeline.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
Instance Method Summary collapse
-
#<<(command) ⇒ Object
Commands added using this method will only be considered successful if their exit status is 0.
-
#add(command, success_codes) ⇒ Object
Adds a command to be executed in the pipeline.
- #commands ⇒ Object
-
#error_messages ⇒ Object
Returns a multi-line String, reporting all STDERR messages received from the commands in the pipeline (if any), along with the SystemCallError (Errno) message for each command which had a non-zero exit status.
-
#initialize ⇒ Pipeline
constructor
A new instance of Pipeline.
-
#run ⇒ Object
Runs the command line from β#pipeline` and collects STDOUT/STDERR.
- #success? ⇒ Boolean
Methods included from Utilities::Helpers
Constructor Details
#initialize ⇒ Pipeline
Returns a new instance of Pipeline.
15 16 17 18 19 20 |
# File 'lib/backup/pipeline.rb', line 15 def initialize @commands = [] @success_codes = [] @errors = [] @stderr = '' end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/backup/pipeline.rb', line 9 def errors @errors end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
9 10 11 |
# File 'lib/backup/pipeline.rb', line 9 def stderr @stderr end |
Instance Method Details
#<<(command) ⇒ Object
Commands added using this method will only be considered successful if their exit status is 0.
Use #add if successful exit status codes need to be specified.
39 40 41 |
# File 'lib/backup/pipeline.rb', line 39 def <<(command) add(command, [0]) end |
#add(command, success_codes) ⇒ Object
Adds a command to be executed in the pipeline. Each command will be run in the order in which it was added, with itβs output being piped to the next command.
success_codes
must be an Array of Integer exit codes that will be considered successful for the command
.
29 30 31 32 |
# File 'lib/backup/pipeline.rb', line 29 def add(command, success_codes) @commands << command @success_codes << success_codes end |
#commands ⇒ Object
11 12 13 |
# File 'lib/backup/pipeline.rb', line 11 def commands @commands end |
#error_messages ⇒ Object
Returns a multi-line String, reporting all STDERR messages received from the commands in the pipeline (if any), along with the SystemCallError (Errno) message for each command which had a non-zero exit status.
84 85 86 87 88 |
# File 'lib/backup/pipeline.rb', line 84 def @error_messages ||= ( || '') + "The following system errors were returned:\n" + @errors.map {|err| "#{ err.class }: #{ err. }" }.join("\n") end |
#run ⇒ Object
Runs the command line from β#pipeline` and collects STDOUT/STDERR. STDOUT is then parsed to determine the exit status of each command. For each command with a non-zero exit status, a SystemCallError is created and added to @errors. All STDERR output is set in @stderr.
Note that there is no accumulated STDOUT from the commands themselves. Also, the last command should not attempt to write to STDOUT. Any output on STDOUT from the final command will be sent to STDERR. This in itself will not cause #run to fail, but will log warnings when all commands exit with non-zero status.
Use β#success?` to determine if all commands in the pipeline succeeded. If `#success?` returns `false`, use `#error_messages` to get an error report.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/backup/pipeline.rb', line 57 def run Open4.popen4(pipeline) do |pid, stdin, stdout, stderr| pipestatus = stdout.read.gsub("\n", '').split(':').sort pipestatus.each do |status| index, exitstatus = status.split('|').map(&:to_i) unless @success_codes[index].include?(exitstatus) command = command_name(@commands[index]) @errors << SystemCallError.new( "'#{ command }' returned exit code: #{ exitstatus }", exitstatus ) end end @stderr = stderr.read.strip end Logger.warn() if success? && rescue Exception => err raise Error.wrap(err, 'Pipeline failed to execute') end |
#success? ⇒ Boolean
76 77 78 |
# File 'lib/backup/pipeline.rb', line 76 def success? @errors.empty? end |