Class: Aruba::Processes::InProcess

Inherits:
BasicProcess show all
Defined in:
lib/aruba/processes/in_process.rb

Overview

Run command in your ruby process

`InProcess` is not meant for direct use - `InProcess.new` - by users. Only it's public methods are part of the public API of aruba, e.g. `#stdin`, `#stdout`.

Defined Under Namespace

Classes: FakeKernel

Instance Attribute Summary collapse

Attributes inherited from BasicProcess

#environment, #exit_timeout, #io_wait_timeout, #startup_wait_time, #working_directory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicProcess

#after_run, #before_run, #commandline, #content, #filesystem_status, #inspect, #output, #restart, #send_signal, #started?, #stopped?, #timed_out?, #wait

Constructor Details

#initialize(cmd, exit_timeout, io_wait_timeout, working_directory, environment = ENV.to_hash.dup, main_class = nil, stop_signal = nil, startup_wait_time = 0) ⇒ InProcess

Returns a new instance of InProcess.


43
44
45
46
47
48
49
50
51
52
# File 'lib/aruba/processes/in_process.rb', line 43

def initialize(cmd, exit_timeout, io_wait_timeout, working_directory, environment = ENV.to_hash.dup, main_class = nil, stop_signal = nil, startup_wait_time = 0)
  @cmd               = cmd
  @argv              = arguments
  @stdin             = StringIO.new
  @stdout            = StringIO.new
  @stderr            = StringIO.new
  @kernel            = FakeKernel.new

  super
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status


23
24
25
# File 'lib/aruba/processes/in_process.rb', line 23

def exit_status
  @exit_status
end

#main_classObject (readonly)


41
42
43
# File 'lib/aruba/processes/in_process.rb', line 41

def main_class
  @main_class
end

Class Method Details

.match?(mode) ⇒ Boolean

Use only if mode is in_process

Returns:

  • (Boolean)

19
20
21
# File 'lib/aruba/processes/in_process.rb', line 19

def self.match?(mode)
  mode == :in_process || (mode.is_a?(Class) && mode <= InProcess)
end

Instance Method Details

#close_io(name) ⇒ Object

Close io


103
104
105
106
107
# File 'lib/aruba/processes/in_process.rb', line 103

def close_io(name)
  fail ArgumentError, 'Only stdin stdout and stderr are allowed to close' unless [:stdin, :stdout, :stderr].include? name

  get_instance_variable(name.to_sym).close
end

#pidObject

Output pid of process

This is the PID of the ruby process! So be careful


117
118
119
# File 'lib/aruba/processes/in_process.rb', line 117

def pid
  $PROCESS_ID
end

#startObject

Start command


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aruba/processes/in_process.rb', line 55

def start
  fail "You need to call aruba.config.main_class = YourMainClass" unless main_class

  @started = true

  Dir.chdir @working_directory do
    before_run

    Aruba.platform.with_environment environment.merge('PWD' => @working_directory) do
      main_class.new(@argv, @stdin, @stdout, @stderr, @kernel).execute!
    end

    after_run

    yield self if block_given?
  end
end

#stderrObject

Access stderr


90
91
92
# File 'lib/aruba/processes/in_process.rb', line 90

def stderr(*)
  @stderr.string
end

#stdinObject

Access stdin


80
81
82
# File 'lib/aruba/processes/in_process.rb', line 80

def stdin
  @stdin.string
end

#stdoutObject

Access stdout


85
86
87
# File 'lib/aruba/processes/in_process.rb', line 85

def stdout(*)
  @stdout.string
end

#stopObject

Stop command


74
75
76
77
# File 'lib/aruba/processes/in_process.rb', line 74

def stop(*)
  @started     = false
  @exit_status = @kernel.exitstatus
end

#terminateObject

Terminate program


110
111
112
# File 'lib/aruba/processes/in_process.rb', line 110

def terminate
  stop
end

#write(input) ⇒ Object

Write strint to stdin

Parameters:

  • input (String)

    Write string to stdin in


98
99
100
# File 'lib/aruba/processes/in_process.rb', line 98

def write(input)
  @stdin.write input
end