Class: Aruba::Processes::SpawnProcess

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

Overview

Spawn a process for command

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

Instance Attribute Summary

Attributes inherited from BasicProcess

#environment, #exit_status, #exit_timeout, #io_wait_timeout, #main_class, #startup_wait_time, #working_directory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicProcess

#after_run, #before_run, #commandline, #inspect, #output, #restart, #started?, #stopped?, #timed_out?

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) ⇒ SpawnProcess

Create process

Parameters:

  • cmd (String)

    Command string

  • exit_timeout (Integer)

    The timeout until we expect the command to be finished

  • io_wait_timeout (Integer)

    The timeout until we expect the io to be finished

  • working_directory (String)

    The directory where the command will be executed

  • environment (Hash) (defaults to: ENV.to_hash.dup)

    Environment variables

  • main_class (Class) (defaults to: nil)

    E.g. Cli::App::Runner

  • stop_signal (String) (defaults to: nil)

    Name of signal to send to stop process. E.g. 'HUP'.

  • startup_wait_time (Integer) (defaults to: 0)

    The amount of seconds to wait after Aruba has started a command.


51
52
53
54
55
56
57
# File 'lib/aruba/processes/spawn_process.rb', line 51

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)
  super

  @process      = nil
  @stdout_cache = nil
  @stderr_cache = nil
end

Class Method Details

.match?(_mode) ⇒ Boolean

Use as default launcher

Returns:

  • (Boolean)

22
23
24
# File 'lib/aruba/processes/spawn_process.rb', line 22

def self.match?(_mode)
  true
end

Instance Method Details

#close_io(name) ⇒ Object

Close io


164
165
166
167
168
# File 'lib/aruba/processes/spawn_process.rb', line 164

def close_io(name)
  return if stopped?

  @process.io.public_send(name.to_sym).close
end

#contentString

Content of command

Returns:

  • (String)

    The content of the script/command. This might be binary output as string if your command is a binary executable.


248
249
250
# File 'lib/aruba/processes/spawn_process.rb', line 248

def content
  File.read command_string.to_s
end

#filesystem_statusAruba::Platforms::FilesystemStatus

Return file system stats for the given command

Returns:


239
240
241
# File 'lib/aruba/processes/spawn_process.rb', line 239

def filesystem_status
  Aruba.platform.filesystem_status.new(command_string.to_s)
end

#pidObject

Output pid of process

This is the PID of the spawned process.


219
220
221
# File 'lib/aruba/processes/spawn_process.rb', line 219

def pid
  @process.pid
end

#send_signal(signal) ⇒ Object

Send command a signal

Parameters:

  • signal (String)

    The signal, i.e. 'TERM'


227
228
229
230
231
232
233
# File 'lib/aruba/processes/spawn_process.rb', line 227

def send_signal(signal)
  fail CommandAlreadyStoppedError, %(Command "#{commandline}" with PID "#{pid}" has already stopped.) if @process.exited?

  Process.kill signal, pid
rescue Errno::ESRCH
  raise CommandAlreadyStoppedError, %(Command "#{commandline}" with PID "#{pid}" has already stopped.)
end

#start {|SpawnProcess| ... } ⇒ Object

Run the command

rubocop:disable Metrics/MethodLength

Yields:


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aruba/processes/spawn_process.rb', line 65

def start
  # rubocop:disable Metrics/LineLength
  fail CommandAlreadyStartedError, %(Command "#{commandline}" has already been started. Please `#stop` the command first and `#start` it again. Alternatively use `#restart`.\n#{caller.join("\n")}) if started?
  # rubocop:enable Metrics/LineLength

  @started = true

  @process = ChildProcess.build(*command_string.to_a, *arguments)
  @stdout_file = Tempfile.new('aruba-stdout-')
  @stderr_file = Tempfile.new('aruba-stderr-')

  @stdout_file.sync = true
  @stderr_file.sync = true

  @stdout_file.binmode
  @stderr_file.binmode

  @exit_status = nil
  @duplex      = true

  before_run

  @process.leader    = true
  @process.io.stdout = @stdout_file
  @process.io.stderr = @stderr_file
  @process.duplex    = @duplex
  @process.cwd       = @working_directory

  @process.environment.update(environment)

  begin
    Aruba.platform.with_environment(environment) do
      @process.start
      sleep startup_wait_time
    end
  rescue ChildProcess::LaunchError => e
    raise LaunchError, "It tried to start #{commandline}. " + e.message
  end

  after_run

  yield self if block_given?
end

#stderr(opts = {}) ⇒ String

Access to stderr of process

Parameters:

  • opts (Hash) (defaults to: {})

    Options

  • [Integer] (Hash)

    a customizable set of options

Returns:

  • (String)

    The content of stderr


145
146
147
148
149
150
151
152
# File 'lib/aruba/processes/spawn_process.rb', line 145

def stderr(opts = {})
  return @stderr_cache if stopped?

  wait_for_io opts.fetch(:wait_for_io, io_wait_timeout) do
    @process.io.stderr.flush
    open(@stderr_file.path).read
  end
end

#stdinObject

Access to stdin of process


110
111
112
113
114
# File 'lib/aruba/processes/spawn_process.rb', line 110

def stdin
  return if @process.exited?

  @process.io.stdin
end

#stdout(opts = {}) ⇒ String

Access to stdout of process

Parameters:

  • opts (Hash) (defaults to: {})

    Options

  • [Integer] (Hash)

    a customizable set of options

Returns:

  • (String)

    The content of stdout


126
127
128
129
130
131
132
133
# File 'lib/aruba/processes/spawn_process.rb', line 126

def stdout(opts = {})
  return @stdout_cache if stopped?

  wait_for_io opts.fetch(:wait_for_io, io_wait_timeout) do
    @process.io.stdout.flush
    open(@stdout_file.path).read
  end
end

#stopObject

Stop command


171
172
173
174
175
176
177
178
179
180
181
# File 'lib/aruba/processes/spawn_process.rb', line 171

def stop(*)
  return @exit_status if stopped?

  begin
    @process.poll_for_exit(@exit_timeout)
  rescue ChildProcess::TimeoutError
    @timed_out = true
  end

  terminate
end

#terminateObject

Terminate command


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/aruba/processes/spawn_process.rb', line 189

def terminate
  return @exit_status if stopped?

  unless @process.exited?
    if @stop_signal
      # send stop signal ...
      send_signal @stop_signal
      # ... and set the exit status
      wait
    else
      @process.stop
    end
  end

  @exit_status  = @process.exit_code

  @stdout_cache = read_temporary_output_file @stdout_file
  @stderr_cache = read_temporary_output_file @stderr_file

  # @stdout_file = nil
  # @stderr_file = nil

  @started = false

  @exit_status
end

#waitObject

Wait for command to finish


184
185
186
# File 'lib/aruba/processes/spawn_process.rb', line 184

def wait
  @process.wait
end

#write(input) ⇒ Object


154
155
156
157
158
159
160
161
# File 'lib/aruba/processes/spawn_process.rb', line 154

def write(input)
  return if stopped?

  @process.io.stdin.write(input)
  @process.io.stdin.flush

  self
end