Class: LCR::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/long-command-runner/container.rb

Overview

This class aims to contain the runing script.

Constant Summary collapse

ENVIRONMENT =

The expected Language environment:

{ 'LANGUAGE' => 'en' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, opts = {}) ⇒ Container

Initializer takes the command as a plain string. it imediatly launch the command

Parameters:

  • command (String)

    The command to execute.

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

    The option to pass to Open3.popen3.



39
40
41
42
# 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

Instance Attribute Details

#stderrIO (readonly)

Note:

Be aware that it may be closed by the process (on exit), or by you. We don’t monitor those states here.

Get the standard error IO of the process.

Returns:

  • (IO)


33
34
35
# File 'lib/long-command-runner/container.rb', line 33

def stderr
  @stderr
end

#stdinIO (readonly)

Note:

Be aware that it may be closed by the process (on exit), or by you. We don’t monitor those states here.

Get the standard input IO of the process.

Returns:

  • (IO)


17
18
19
# File 'lib/long-command-runner/container.rb', line 17

def stdin
  @stdin
end

#stdoutIO (readonly)

Note:

Be aware that it may be closed by the process (on exit), or by you. We don’t monitor those states here.

Get the standard output IO of the process.

Returns:

  • (IO)


25
26
27
# File 'lib/long-command-runner/container.rb', line 25

def stdout
  @stdout
end

Instance Method Details

#childrenObject



72
73
74
75
76
77
# 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

#pidObject

Return the pid of the process



68
69
70
# File 'lib/long-command-runner/container.rb', line 68

def pid
  @wait_thr.pid
end

#running?Boolean

Is the last launched command is still running.

Returns:

  • (Boolean)


45
46
47
# File 'lib/long-command-runner/container.rb', line 45

def running?
  @wait_thr.alive?
end

#statusProcess:Status?

Get the status of the process without blocking.

Returns:

  • (Process:Status, nil)

    The exit status of the process if it is finished. if the Process isn’t finished it return nil.



53
54
55
56
57
# File 'lib/long-command-runner/container.rb', line 53

def status
  return nil if running?

  @wait_thr.value
end

#waitProcess::Status

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

Returns:

  • (Process::Status)


63
64
65
# File 'lib/long-command-runner/container.rb', line 63

def wait
  @wait_thr.value
end