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, #stop_signal, #working_directory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicProcess

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

Constructor Details

#initialize(cmd, exit_timeout, io_wait_timeout, working_directory, environment = Aruba.platform.environment_variables.hash_from_env, main_class = nil, stop_signal = nil, startup_wait_time = 0) ⇒ InProcess

rubocop:disable Metrics/ParameterLists



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aruba/processes/in_process.rb', line 49

def initialize(cmd, exit_timeout, io_wait_timeout, working_directory, # rubocop:disable Metrics/ParameterLists
               environment = Aruba.platform.environment_variables.hash_from_env,
               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)



47
48
49
# File 'lib/aruba/processes/in_process.rb', line 47

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



111
112
113
114
115
116
117
# File 'lib/aruba/processes/in_process.rb', line 111

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

  get_instance_variable(name.to_sym).close
end

#interactive?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/aruba/processes/in_process.rb', line 131

def interactive?
  false
end

#pidObject

Output pid of process

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



127
128
129
# File 'lib/aruba/processes/in_process.rb', line 127

def pid
  $PROCESS_ID
end

#startObject

Start command



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/aruba/processes/in_process.rb', line 63

def start
  raise "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



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

def stderr(*)
  @stderr.string
end

#stdinObject

Access stdin



88
89
90
# File 'lib/aruba/processes/in_process.rb', line 88

def stdin
  @stdin.string
end

#stdoutObject

Access stdout



93
94
95
# File 'lib/aruba/processes/in_process.rb', line 93

def stdout(*)
  @stdout.string
end

#stopObject

Stop command



82
83
84
85
# File 'lib/aruba/processes/in_process.rb', line 82

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

#terminateObject

Terminate program



120
121
122
# File 'lib/aruba/processes/in_process.rb', line 120

def terminate
  stop
end

#write(input) ⇒ Object

Write strint to stdin

Parameters:

  • input (String)

    Write string to stdin in



106
107
108
# File 'lib/aruba/processes/in_process.rb', line 106

def write(input)
  @stdin.write input
end