Class: AssLauncher::Support::Shell::ProcessHolder Private
- Inherits:
-
Object
- Object
- AssLauncher::Support::Shell::ProcessHolder
- Includes:
- Platforms
- Defined in:
- lib/ass_launcher/support/shell/process_holder.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
WARNIG!!! not forgot kill of threads created and handled of ProcessHolder
For run command used popen3 whith command_string and *args. It not shell running. If command.args.size == 0 in command.args array will be pushed one empty string. For more info see Process.spawn documentation
Class for running Command in subprocess and controlling him. Process run command in the thread. Thread waiting for process exit and call Command#exit_handling
Defined Under Namespace
Classes: KillProcessError, ProcessNotRunning, RunProcessError
Instance Attribute Summary collapse
-
#command ⇒ Command, Script
readonly
Command runned in process.
- #options ⇒ Object readonly private
-
#pid ⇒ Fixnum
readonly
Pid of runned process.
- #popen3_thread ⇒ Object readonly private
-
#result ⇒ RunAssResult
readonly
Result of execution command.
-
#thread ⇒ Thread
readonly
private
Thread waiting for process.
Class Method Summary collapse
- .cmd_exe_with_c?(command) ⇒ Boolean private
- .cmd_exe_with_k?(command) ⇒ Boolean private
-
.process_list ⇒ Arry<ProcessHolder>
Keep of created instaces.
-
.run(command, options = {}) ⇒ ProcessHolder
Run command subprocess in new Thread and return instace for process controlling Thread wait process and handling process exit wihth Command#exit_handling.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
True if thread alive.
-
#initialize(command, options = {}) ⇒ ProcessHolder
constructor
private
A new instance of ProcessHolder.
-
#kill ⇒ self
Kill the process.
- #run ⇒ self private
- #running? ⇒ Boolean private
-
#wait ⇒ self
Wait for thread exit.
Methods included from Platforms
cygwin?, env, glob, linux?, path, path_class, windows?
Constructor Details
#initialize(command, options = {}) ⇒ ProcessHolder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ProcessHolder.
126 127 128 129 130 131 132 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 126 def initialize(command, = {}) fail ArgumentError, 'Command was already running' if command.running? @command = command command.send(:process_holder=, self) @options = [:new_pgroup] = true if windows? end |
Instance Attribute Details
#command ⇒ Command, Script (readonly)
Returns command runned in process.
62 63 64 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 62 def command @command end |
#options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
69 70 71 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 69 def @options end |
#pid ⇒ Fixnum (readonly)
Returns pid of runned process.
54 55 56 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 54 def pid @pid end |
#popen3_thread ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
69 70 71 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 69 def popen3_thread @popen3_thread end |
#result ⇒ RunAssResult (readonly)
Returns result of execution command.
58 59 60 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 58 def result @result end |
#thread ⇒ Thread (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns thread waiting for process.
66 67 68 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 66 def thread @thread end |
Class Method Details
.cmd_exe_with_c?(command) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
‘cmd /C command` not kill command when cmd killed
99 100 101 102 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 99 def self.cmd_exe_with_c?(command) shell_str = "#{command.cmd} #{command.args.join(' ')}" ! (shell_str =~ %r{(?<=\W|\A)cmd(.exe)?\s*(\/C)}i).nil? end |
.cmd_exe_with_k?(command) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
‘cmd /K command` not exit when exit command. Thread hangup
92 93 94 95 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 92 def self.cmd_exe_with_k?(command) shell_str = "#{command.cmd} #{command.args.join(' ')}" ! (shell_str =~ %r{(?<=\W|\A)cmd(.exe)?\s*(\/K)}i).nil? end |
.process_list ⇒ Arry<ProcessHolder>
Keep of created instaces
76 77 78 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 76 def self.process_list @@process_slist ||= [] end |
.run(command, options = {}) ⇒ ProcessHolder
WARNIG!!! not forgot kill of threads created and handled of ProcessHolder
For run command used popen3 whith command_string and *args. It not shell running. If command.args.size == 0 in command.args array will be pushed one empty string. For more info see Process.spawn documentation
Run command subprocess in new Thread and return instace for process controlling Thread wait process and handling process exit wihth Command#exit_handling
116 117 118 119 120 121 122 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 116 def self.run(command, = {}) fail RunProcessError, 'Forbidden run cmd.exe with /K key'\ if cmd_exe_with_k? command h = new(command, ) reg_process h h.run end |
Instance Method Details
#alive? ⇒ Boolean
True if thread alive
205 206 207 208 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 205 def alive? fail ProcessNotRunning unless running? thread.alive? end |
#kill ⇒ self
WARNIG! for command runned as cmd /C commnd can’t get pid of command process. In this case error raised
Kill the process
184 185 186 187 188 189 190 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 184 def kill return self unless alive? fail KillProcessError, 'Can\'t kill subprocess runned in cmd.exe '\ 'on the windows machine' if self.class.cmd_exe_with_c? command Process.kill('KILL', pid) wait end |
#run ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
136 137 138 139 140 141 142 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 136 def run fail RunProcessError, "Process was run. Pid: #{pid}" if running? @popen3_thread, stdout, stderr = run_process @pid = @popen3_thread.pid @thread = wait_process_in_thread(stdout, stderr) self end |
#running? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
144 145 146 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 144 def running? ! pid.nil? end |
#wait ⇒ self
Wait for thread exit
196 197 198 199 200 |
# File 'lib/ass_launcher/support/shell/process_holder.rb', line 196 def wait return self unless alive? thread.join self end |