Sha256: df536c4569500f9b836c180d9e82566b074440cd6b1919028f536e6b147946b5

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require 'childprocess'
require 'tempfile'
require 'shellwords'
require 'aruba/errors'

module Aruba
  class SpawnProcess
    include Shellwords

    def initialize(cmd, exit_timeout, io_wait)
      @exit_timeout = exit_timeout
      @io_wait = io_wait

      @cmd = cmd
      @process = nil
      @exit_code = nil
    end

    def run!(&block)
      @process = ChildProcess.build(*shellwords(@cmd))
      @out = Tempfile.new("aruba-out")
      @err = Tempfile.new("aruba-err")
      @process.io.stdout = @out
      @process.io.stderr = @err
      @process.duplex = true
      @exit_code = nil
      begin
        @process.start
      rescue ChildProcess::LaunchError => e
        raise LaunchError.new(e.message)
      end
      yield self if block_given?
    end

    def stdin
      @process.io.stdin
    end

    def output
      stdout + stderr
    end

    def stdout
      wait_for_io do
        @out.rewind
        @out.read
      end
    end

    def stderr
      wait_for_io do
        @err.rewind
        @err.read
      end
    end

    def read_stdout
      wait_for_io do
        @process.io.stdout.flush
        open(@out.path).read
      end
    end

    def stop(reader)
      return @exit_code unless @process
      unless @process.exited?
        @process.poll_for_exit(@exit_timeout)
      end
      reader.stdout stdout
      reader.stderr stderr
      @exit_code = @process.exit_code
      @process = nil
      @exit_code
    end

    def terminate
      if @process
        stdout && stderr # flush output
        @process.stop
        stdout && stderr # flush output
      end
    end

    private

    def wait_for_io(&block)
      sleep @io_wait if @process
      yield
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aruba-0.5.2 lib/aruba/spawn_process.rb