lib/backup/pipeline.rb in backup-4.4.1 vs lib/backup/pipeline.rb in backup-5.0.0.beta.1

- old
+ new

@@ -1,7 +1,5 @@ -# encoding: utf-8 - module Backup class Pipeline class Error < Backup::Error; end include Utilities::Helpers @@ -10,11 +8,11 @@ def initialize @commands = [] @success_codes = [] @errors = [] - @stderr = '' + @stderr = "" end ## # Adds a command to be executed in the pipeline. # Each command will be run in the order in which it was added, @@ -49,26 +47,25 @@ # 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. def run - Open4.popen4(pipeline) do |pid, stdin, stdout, stderr| - pipestatus = stdout.read.gsub("\n", '').split(':').sort + Open4.popen4(pipeline) do |_pid, _stdin, stdout, stderr| + pipestatus = stdout.read.delete("\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 + index, exitstatus = status.split("|").map(&:to_i) + next if @success_codes[index].include?(exitstatus) + command = command_name(@commands[index]) + @errors << SystemCallError.new( + "'#{command}' returned exit code: #{exitstatus}", exitstatus + ) end @stderr = stderr.read.strip end Logger.warn(stderr_messages) if success? && stderr_messages rescue Exception => err - raise Error.wrap(err, 'Pipeline failed to execute') + raise Error.wrap(err, "Pipeline failed to execute") end def success? @errors.empty? end @@ -76,13 +73,13 @@ ## # 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. def error_messages - @error_messages ||= (stderr_messages || '') + - "The following system errors were returned:\n" + - @errors.map {|err| "#{ err.class }: #{ err.message }" }.join("\n") + @error_messages ||= (stderr_messages || "") + + "The following system errors were returned:\n" + + @errors.map { |err| "#{err.class}: #{err.message}" }.join("\n") end private ## @@ -104,21 +101,20 @@ # may be interleaved. Interleaving of the "index|exit status" outputs # should not be an issue, given the small byte size of the data being written. def pipeline parts = [] @commands.each_with_index do |command, index| - parts << %Q[{ #{ command } 2>&4 ; echo "#{ index }|$?:" >&3 ; }] + parts << %({ #{command} 2>&4 ; echo "#{index}|$?:" >&3 ; }) end - %Q[{ #{ parts.join(' | ') } } 3>&1 1>&2 4>&2] + %({ #{parts.join(" | ")} } 3>&1 1>&2 4>&2) end def stderr_messages - @stderr_messages ||= @stderr.empty? ? false : <<-EOS.gsub(/^ +/, ' ') + @stderr_messages ||= @stderr.empty? ? false : <<-EOS.gsub(/^ +/, " ") Pipeline STDERR Messages: (Note: may be interleaved if multiple commands returned error messages) - #{ @stderr } + #{@stderr} EOS end - end end