lib/ztk/command.rb in ztk-1.5.5 vs lib/ztk/command.rb in ztk-1.6.0

- old
+ new

@@ -1,8 +1,7 @@ require 'ostruct' require 'timeout' -require 'childprocess' require 'socket' module ZTK # Command Error Class @@ -46,12 +45,10 @@ :ignore_exit_status => false, :exit_code => 0, :silence => false }.merge(configuration)) config.ui.logger.debug { "config=#{config.send(:table).inspect}" } - - ChildProcess.posix_spawn = false end # Execute Command # # @example Execute a command: @@ -96,30 +93,39 @@ parent_stdout_reader, child_stdout_writer = IO.pipe parent_stderr_reader, child_stderr_writer = IO.pipe start_time = Time.now.utc - proc = ChildProcess.build(*command) - proc.io.stdout = child_stdout_writer - proc.io.stderr = child_stderr_writer - proc.start + pid = Process.fork do + parent_stdout_reader.close + parent_stderr_reader.close + STDOUT.reopen(child_stdout_writer) + STDERR.reopen(child_stderr_writer) + STDIN.reopen("/dev/null") + + child_stdout_writer.close + child_stderr_writer.close + + Kernel.exec(command) + end child_stdout_writer.close child_stderr_writer.close reader_writer_key = {parent_stdout_reader => :stdout, parent_stderr_reader => :stderr} reader_writer_map = {parent_stdout_reader => options.ui.stdout, parent_stderr_reader => options.ui.stderr} direct_log(:info) { log_header("COMMAND") } - direct_log(:info) { "#{command}\n" } + direct_log(:info) { "#{command.inspect}\n" } direct_log(:info) { log_header("STARTED") } begin Timeout.timeout(options.timeout) do loop do - reader_writer_map.keys.each do |pipe| - data = (pipe.readpartial(1024) rescue nil) + pipes = IO.select(reader_writer_map.keys, [], reader_writer_map.keys).first + pipes.each do |pipe| + data = pipe.read if (data.nil? || data.empty?) sleep(0.1) next end @@ -146,20 +152,20 @@ output += data options.on_progress.nil? or options.on_progress.call end + break if reader_writer_map.keys.all?{ |reader| reader.eof? } end end rescue Timeout::Error => e direct_log(:fatal) { log_header("TIMEOUT") } log_and_raise(CommandError, "Process timed out after #{options.timeout} seconds!") - proc.stop end - proc.wait - exit_code = proc.exit_code + Process.waitpid(pid) + exit_code = $?.exitstatus direct_log(:info) { log_header("STOPPED") } parent_stdout_reader.close parent_stderr_reader.close