class Arrival::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/arrival/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/arrival/command.rb, line 25 def run 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/arrival/command.rb, line 84 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/arrival/command.rb, line 65 def full_command "#{command_line} 2> #{error_log_path}" end
Prints a line break to keep the logs separate from the execution time print by the migration
# File lib/arrival/command.rb, line 96 def log_finished logger.write("\n") end
Logs when the execution started
# File lib/arrival/command.rb, line 89 def log_started logger.write("\n") logger.say("Running #{command_line}\n\n", true) end
TODO: Update comment Runs the command in a separate process, capturing its stdout and execution status
# File lib/arrival/command.rb, line 43 def run_in_process stdout = system(full_command) @exit_code = $? # Open3.capture2(full_command) do |_stdin, stdout, waith_thr| # begin # loop do # IO.select([stdout]) # data = stdout.read_nonblock(8192) # logger.write_no_newline(data) # end # rescue EOFError # rubocop:disable Lint/HandleExceptions # # 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/arrival/command.rb, line 74 def validate_status! # binding.pry # raise SignalError.new(status) if status.signaled? # rubocop:disable Style/RaiseArgs # raise CommandNotFoundError if status.exitstatus == COMMAND_NOT_FOUND # raise Error, error_message unless status.success? end