lib/buildbox/command.rb in buildbox-0.6.2 vs lib/buildbox/command.rb in buildbox-0.7.beta1
- old
+ new
@@ -24,13 +24,13 @@
command.start(&block)
command
end
def initialize(*args)
- @options = args.last.is_a?(Hash) ? args.pop : {}
+ @options = args.last.is_a?(Hash) ? args.pop : {}
@arguments = args.dup
- @logger = Buildbox.logger
+ @logger = Buildbox.logger
end
def arguments
[ *@arguments ].compact.map(&:to_s) # all arguments must be a string
end
@@ -53,58 +53,54 @@
PTY.open
rescue
IO.pipe
end
- process.io.stdout = write_pipe
- process.io.stderr = write_pipe
- process.duplex = true
+ process.io.stdout = write_pipe
+ process.io.stderr = write_pipe
+ process.duplex = true
# Set the environment on the process
if @options[:environment]
@options[:environment].each_pair do |key, value|
process.environment[key] = value
end
end
+ # Record the start time for timeout purposes
+ start_time = Time.now.to_i
+
+ # Track the output as it goes
+ output = ""
+
# Start the process
process.start
+ @logger.debug("Process #{arguments} started with PID: #{process.pid}")
+
# Make sure the stdin does not buffer
process.io.stdin.sync = true
- @logger.debug("Process #{arguments} started with PID: #{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
- # Record the start time for timeout purposes
- start_time = Time.now.to_i
-
- # Track the output as it goes
- output = ""
-
- @logger.debug("Selecting on IO")
while true
results = IO.select([read_pipe], nil, nil, timeout || 0.1) || []
readers = results[0]
# Check if we have exceeded our timeout
raise TimeoutExceeded if timeout && (Time.now.to_i - start_time) > timeout
- # Kill the process and wait a bit for it to disappear
- # Process.kill('KILL', process.pid)
- # Process.waitpid2(process.pid)
# Check the readers to see if they're ready
if readers && !readers.empty?
readers.each do |r|
# Read from the IO object
- data = read_io(r)
+ data = read_until_block(r)
# We don't need to do anything if the data is empty
next if data.empty?
output << cleaned_data = UTF8.clean(data)
@@ -134,11 +130,11 @@
# Read the final output data, since it is possible we missed a small
# amount of text between the time we last read data and when the
# process exited.
# Read the extra data
- extra_data = read_io(read_pipe)
+ extra_data = read_until_block(read_pipe)
# If there's some that we missed
if extra_data != ""
output << cleaned_data = UTF8.clean(extra_data)
yield cleaned_data if block_given?
@@ -148,21 +144,21 @@
# On JRuby, we need to close the writers after the process,
# for some reason. See https://github.com/mitchellh/vagrant/pull/711
write_pipe.close
end
- @output = output.chomp
+ @output = output.chomp
@exit_status = process.exit_code
end
private
# Reads data from an IO object while it can, returning the data it reads.
# When it encounters a case when it can't read anymore, it returns the
# data.
#
# @return [String]
- def read_io(io)
+ def read_until_block(io)
data = ""
while true
begin
if Platform.windows?