Class: Command::Runner::Backends::Spawn
- Defined in:
- lib/command/runner/backends/spawn.rb
Overview
Spawns a process using ruby’s Process.spawn.
Direct Known Subclasses
Class Method Summary collapse
-
.available?(_ = false) ⇒ Boolean
Returns whether or not this backend is available on this platform.
Instance Method Summary collapse
-
#call(command, arguments, env = {}, options = {}) {|Message| ... } ⇒ Message, Object
Run the given command and arguments, in the given environment.
-
#initialize ⇒ Spawn
constructor
Initialize the backend.
-
#spawn(env, command, arguments, options) ⇒ Numeric
Spawn the given process, in the environment with the given options.
-
#wait2(process_id = -1)) ⇒ Array<(Numeric, Process::Status)>
Waits for the given process, and returns the process id and the status.
Methods inherited from Fake
#ran?, unsafe?, #unsafe?, unsafe_execution?
Constructor Details
#initialize ⇒ Spawn
Initialize the backend.
19 20 21 |
# File 'lib/command/runner/backends/spawn.rb', line 19 def initialize super end |
Class Method Details
.available?(_ = false) ⇒ Boolean
Returns whether or not this backend is available on this platform. Process.spawn doesn’t work the way we want it to on JRuby 1.9 mode, so we prevent this from being used on that platform.
14 15 16 |
# File 'lib/command/runner/backends/spawn.rb', line 14 def self.available?(_ = false) Process.respond_to?(:spawn) && !(RUBY_PLATFORM == "java" && RUBY_VERSION =~ /\A1\.9/) end |
Instance Method Details
#call(command, arguments, env = {}, options = {}) {|Message| ... } ⇒ Message, Object
The block is called in another thread, so in ruby versions other than MRI, make sure your code is thread-safe.
Run the given command and arguments, in the given environment.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/command/runner/backends/spawn.rb', line 32 def call(command, arguments, env = {}, = {}, &block) super stderr_r, stderr_w = IO.pipe stdout_r, stdout_w = IO.pipe stdin_r, stdin_w = IO.pipe if [:input] stdin_w.write(.delete(:input)) end = .merge(:in => stdin_r, :out => stdout_w, :err => stderr_w) stdin_w.close line = [command, *arguments].join(' ') start_time = Time.now process_id = spawn(env, command, arguments, ) future do _, status = wait2(process_id) end_time = Time.now [stdout_w, stderr_w].each(&:close) = Message.new :process_id => process_id, :exit_code => status.exitstatus, :finished => true, :time => (start_time - end_time).abs, :env => env, :options => , :stdout => stdout_r.read, :stderr => stderr_r.read, :line => line, :executed => true, :status => status if block_given? block.call() else end end end |
#spawn(env, command, arguments, options) ⇒ Numeric
Spawn the given process, in the environment with the given options.
82 83 84 85 86 87 88 |
# File 'lib/command/runner/backends/spawn.rb', line 82 def spawn(env, command, arguments, ) if .delete(:unsafe) Process.spawn(env, "#{command} #{arguments.join(' ')}", ) else Process.spawn(env, command, *arguments, ) end end |
#wait2(process_id = -1)) ⇒ Array<(Numeric, Process::Status)>
Waits for the given process, and returns the process id and the status.
95 96 97 |
# File 'lib/command/runner/backends/spawn.rb', line 95 def wait2(process_id = -1) Process.wait2(process_id) end |