Class: LCR::Container
- Inherits:
-
Object
- Object
- LCR::Container
- 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
-
#stderr ⇒ IO
readonly
Get the standard error IO of the process.
-
#stdin ⇒ IO
readonly
Get the standard input IO of the process.
-
#stdout ⇒ IO
readonly
Get the standard output IO of the process.
Instance Method Summary collapse
- #children ⇒ Object
-
#initialize(command, opts = {}) ⇒ Container
constructor
Initializer takes the command as a plain string.
-
#pid ⇒ Object
Return the pid of the process.
-
#running? ⇒ Boolean
Is the last launched command is still running.
-
#status ⇒ Process:Status?
Get the status of the process without blocking.
-
#wait ⇒ Process::Status
Wait and return the process exit status.
Constructor Details
#initialize(command, opts = {}) ⇒ Container
Initializer takes the command as a plain string. it imediatly launch the command
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
#stderr ⇒ IO (readonly)
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.
33 34 35 |
# File 'lib/long-command-runner/container.rb', line 33 def stderr @stderr end |
#stdin ⇒ IO (readonly)
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.
17 18 19 |
# File 'lib/long-command-runner/container.rb', line 17 def stdin @stdin end |
#stdout ⇒ IO (readonly)
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.
25 26 27 |
# File 'lib/long-command-runner/container.rb', line 25 def stdout @stdout end |
Instance Method Details
#children ⇒ Object
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 |
#pid ⇒ Object
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.
45 46 47 |
# File 'lib/long-command-runner/container.rb', line 45 def running? @wait_thr.alive? end |
#status ⇒ Process:Status?
Get the status of the process without blocking.
53 54 55 56 57 |
# File 'lib/long-command-runner/container.rb', line 53 def status return nil if running? @wait_thr.value end |
#wait ⇒ Process::Status
Wait and return the process exit status. This method is blocking until the process if finished.
63 64 65 |
# File 'lib/long-command-runner/container.rb', line 63 def wait @wait_thr.value end |