lib/cocaine/command_line/runners/posix_runner.rb in cocaine-0.5.1 vs lib/cocaine/command_line/runners/posix_runner.rb in cocaine-0.5.2
- old
+ new
@@ -1,30 +1,48 @@
# coding: UTF-8
module Cocaine
class CommandLine
class PosixRunner
- if Cocaine::CommandLine.posix_spawn_available?
-
- def call(command, env = {})
- input, output = IO.pipe
- pid = spawn(env, command, :out => output)
- output.close
- result = input.read
- waitpid(pid)
- result
+ def self.available?
+ begin
+ require 'posix/spawn'
+ true
+ rescue LoadError => e
+ false
end
+ end
- private
+ def self.supported?
+ available? && !Cocaine::CommandLine.java?
+ end
- def spawn(*args)
- POSIX::Spawn.spawn(*args)
- end
+ def supported?
+ self.class.supported?
+ end
- def waitpid(pid)
- Process.waitpid(pid)
+ def call(command, env = {})
+ input, output = IO.pipe
+ pid = spawn(env, command, :out => output)
+ output.close
+ result = ""
+ while partial_result = input.read(8192)
+ result << partial_result
end
+ waitpid(pid)
+ input.close
+ result
+ end
+ private
+
+ def spawn(*args)
+ POSIX::Spawn.spawn(*args)
end
+
+ def waitpid(pid)
+ Process.waitpid(pid)
+ end
+
end
end
end