lib/ztk/command.rb in ztk-1.5.1 vs lib/ztk/command.rb in ztk-1.5.2
- old
+ new
@@ -1,8 +1,9 @@
require 'ostruct'
require 'timeout'
require 'childprocess'
+require 'socket'
module ZTK
# Command Error Class
#
@@ -115,12 +116,16 @@
begin
Timeout.timeout(options.timeout) do
loop do
reader_writer_map.keys.each do |pipe|
data = (pipe.readpartial(1024) rescue nil)
- next if (data.nil? || data.empty?)
+ if (data.nil? || data.empty?)
+ sleep(0.1)
+ next
+ end
+
case reader_writer_key[pipe]
when :stdout then
if !stdout_header
direct_log(:info) { log_header("STDOUT") }
stdout_header = true
@@ -140,11 +145,10 @@
end
output += data
end
break if reader_writer_map.keys.all?{ |reader| reader.eof? }
- sleep(0.1)
end
end
rescue Timeout::Error => e
direct_log(:fatal) { log_header("TIMEOUT") }
log_and_raise(CommandError, "Process timed out after #{options.timeout} seconds!")
@@ -182,10 +186,10 @@
private
# Returns a string in the format of "user@hostname" for the current
# shell.
def tag
- @@hostname ||= %x(hostname).split('.').first.strip
+ @@hostname ||= Socket.gethostname.split('.').first.strip
"#{ENV['USER']}@#{@@hostname}"
end
# Formats a header suitable for writing to the direct logger when logging
# sessions.