Module: ScoutAgent::CoreExtensions::ProcessModule
- Defined in:
- lib/scout_agent/core_extensions.rb
Overview
Module extensions for Process.
Instance Method Summary collapse
-
#term_or_kill(child_pid, pause = 5) ⇒ Object
A convenience method to ensure
child_pidis stopped.
Instance Method Details
#term_or_kill(child_pid, pause = 5) ⇒ Object
A convenience method to ensure child_pid is stopped. First a friendly TERM signal is sent. Then, after pause seconds, KILL will be sent if the process has not yet exited. The exit code for the process is returned if it can be determined (nil is returned otherwise).
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/scout_agent/core_extensions.rb', line 160 def term_or_kill(child_pid, pause = 5) %w[TERM KILL].each { |signal| begin ::Process.kill(signal, child_pid) # attempt to stop process rescue Exception # no such process break # the process is stopped end if signal == "TERM" # give them a chance to respond begin Timeout.timeout(pause) { begin return ::Process.wait2(child_pid).last # response to signal rescue Exception => error # no such child raise if error.is_a? Timeout::Error # reraise timeouts return nil # we have already caught the child end } rescue Timeout::Error # the process didn't exit in time # try again with KILL end end } begin ::Process.wait2(child_pid).last rescue Exception # no such child nil # we have already caught the child end end |