class LCR::Container

This class aims to contain the runing script.

Constants

ENVIRONMENT

The expected Language environment:

Attributes

stderr[R]

Get the standard error IO of the process.

@note Be aware that it may be closed by the process (on exit), or by you.

We don't monitor those states here.

@return [IO]

stdin[R]

Get the standard input IO of the process.

@note Be aware that it may be closed by the process (on exit), or by you.

We don't monitor those states here.

@return [IO]

stdout[R]

Get the standard output IO of the process.

@note Be aware that it may be closed by the process (on exit), or by you.

We don't monitor those states here.

@return [IO]

Public Class Methods

new(command, opts = {}) click to toggle source

Initializer takes the command as a plain string. it imediatly launch the command @param [String] command The command to execute. @param [Hash] opts The option to pass to Open3.popen3.

# File lib/long-command-runner/container.rb, line 39
def initialize(command, opts = {})
  safe_command = quote_sanitize(command)
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(ENVIRONMENT, "bash -c \"#{safe_command}\"", opts)
end

Public Instance Methods

children() click to toggle source
# File lib/long-command-runner/container.rb, line 72
def children
  get_children = lambda do |pid|
    `pgrep -P #{pid}`.split("\n").map { |c_pid| [c_pid.to_i, get_children.call(c_pid)] }.to_h
  end
  get_children.call(pid)
end
pid() click to toggle source

Return the pid of the process

# File lib/long-command-runner/container.rb, line 68
def pid
  @wait_thr.pid
end
running?() click to toggle source

Is the last launched command is still running.

# File lib/long-command-runner/container.rb, line 45
def running?
  @wait_thr.alive?
end
status() click to toggle source

Get the status of the process without blocking.

@return [Process:Status, nil] The exit status of the process if it is finished.

if the Process isn't finished it return nil.
# File lib/long-command-runner/container.rb, line 53
def status
  return nil if running?

  @wait_thr.value
end
wait() click to toggle source

Wait and return the process exit status. This method is blocking until the process if finished.

@return [Process::Status]

# File lib/long-command-runner/container.rb, line 63
def wait
  @wait_thr.value
end

Private Instance Methods

quote_sanitize(cmd) click to toggle source
# File lib/long-command-runner/container.rb, line 81
def quote_sanitize(cmd)
  cmd.gsub(/(["])/, '\\"')
end