lib/buildbox/command.rb in buildbox-0.3.3 vs lib/buildbox/command.rb in buildbox-0.3.4

- old
+ new

@@ -11,39 +11,37 @@ # An error which occurs when the process doesn't end within # the given timeout. class TimeoutExceeded < StandardError; end - attr_reader :pid, :output, :exit_status + attr_reader :output, :exit_status def self.run(*args, &block) - options = args.last.is_a?(Hash) ? args.pop : {} - arguments = args.dup - - # Run the command - command = new(arguments, options, &block) + command = new(*args, &block) command.start(&block) command end - def initialize(arguments, options = {}) - @arguments = arguments - @options = options + def initialize(*args) + @options = args.last.is_a?(Hash) ? args.pop : {} + @arguments = args.dup @logger = Buildbox.logger end + def arguments + [ *@arguments ].compact.map(&:to_s) # all arguments must be a string + end + + def process + @process ||= ChildProcess.build(*arguments) + end + def start(&block) # Get the timeout, if we have one timeout = @options[:timeout] - # Build the command we're going to run - arguments = [ *@arguments ].compact.map(&:to_s) # all arguments must be a string - - # Build the ChildProcess - @logger.info("Starting process: #{arguments}") - - process = ChildProcess.build(*arguments) + # Set the directory for the process process.cwd = File.expand_path(@options[:directory] || Dir.pwd) # Create the pipes so we can read the output in real time. PTY # isn't avaible on all platforms (heroku) so we just fallback to IO.pipe # if it's not presetnt. @@ -68,20 +66,19 @@ process.start # Make sure the stdin does not buffer process.io.stdin.sync = true + @logger.debug("Process #{arguments} started with PID: #{process.pid} and Group ID: #{Process.getpgid(process.pid)}") + if RUBY_PLATFORM != "java" # On Java, we have to close after. See down the method... # Otherwise, we close the writer right here, since we're # not on the writing side. write_pipe.close end - # Store the process id for later cancelling! - @pid = process.pid - # Record the start time for timeout purposes start_time = Time.now.to_i # Track the output as it goes output = "" @@ -105,11 +102,11 @@ # We don't need to do anything if the data is empty next if data.empty? output << cleaned_data = UTF8.clean(data) - yield self, cleaned_data if block_given? + yield cleaned_data if block_given? end end # Break out if the process exited. We have to do this before # attempting to write to stdin otherwise we'll get a broken pipe @@ -138,10 +135,10 @@ extra_data = read_io(read_pipe) # If there's some that we missed if extra_data != "" output << cleaned_data = UTF8.clean(extra_data) - yield self, cleaned_data if block_given? + yield cleaned_data if block_given? end if RUBY_PLATFORM == "java" # On JRuby, we need to close the writers after the process, # for some reason. See https://github.com/mitchellh/vagrant/pull/711