class PerconaMigrator::Command
Executes the given command returning it's status and errors
Constants
- COMMAND_NOT_FOUND
Attributes
Public Class Methods
Constructor
@param command_line
[String] @param error_log_path
[String] @param logger [#write_no_newline]
# File lib/percona_migrator/command.rb, line 11 def initialize(command_line, error_log_path, logger) @command_line = command_line @error_log_path = error_log_path @logger = logger end
Public Instance Methods
Executes the command returning its status. It also prints its stdout to the logger and its stderr to the file specified in error_log_path.
@raise [NoStatusError] if the spawned process' status can't be retrieved @raise [SignalError] if the spawned process received a signal @raise [CommandNotFoundError] if pt-online-schema-change can't be found
@return [Process::Status]
# File lib/percona_migrator/command.rb, line 25 def run log_deprecations log_started run_in_process log_finished validate_status! status end
Private Instance Methods
Returns the error message that appeared in the process' stderr
@return [String]
# File lib/percona_migrator/command.rb, line 81 def error_message File.read(error_log_path) end
Builds the actual command including stderr redirection to the specified log file
@return [String]
# File lib/percona_migrator/command.rb, line 63 def full_command "#{command_line} 2> #{error_log_path}" end
# File lib/percona_migrator/command.rb, line 97 def log_deprecations logger.write("\n") logger.write("[DEPRECATION] This gem has been renamed to Departure and will no longer be supported. Please switch to Departure as soon as possible.") end
Prints a line break to keep the logs separate from the execution time print by the migration
# File lib/percona_migrator/command.rb, line 93 def log_finished logger.write("\n") end
Logs when the execution started
# File lib/percona_migrator/command.rb, line 86 def log_started logger.write("\n") logger.say("Running #{command_line}\n\n", true) end
Runs the command in a separate process, capturing its stdout and execution status
# File lib/percona_migrator/command.rb, line 43 def run_in_process Open3.popen3(full_command) do |_stdin, stdout, _stderr, waith_thr| begin loop do IO.select([stdout]) data = stdout.read_nonblock(8) logger.write_no_newline(data) end rescue EOFError # noop ensure @status = waith_thr.value end end end
Validates the status of the execution
@raise [NoStatusError] if the spawned process' status can't be retrieved @raise [SignalError] if the spawned process received a signal @raise [CommandNotFoundError] if pt-online-schema-change can't be found
# File lib/percona_migrator/command.rb, line 72 def validate_status! raise SignalError.new(status) if status.signaled? raise CommandNotFoundError if status.exitstatus == COMMAND_NOT_FOUND raise Error, error_message unless status.success? end