Module: ScoutAgent::CoreExtensions::ProcessModule

Defined in:
lib/scout_agent/core_extensions.rb

Overview

Module extensions for Process.

Constant Summary collapse

TimeoutStopError =

The error used to halt blocks in the loaded version of the Timeout library.

Timeout.const_defined?(:ExitException) ?
Timeout::ExitException               :
Timeout::Error

Instance Method Summary collapse

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).



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/scout_agent/core_extensions.rb', line 168

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  # signal response
          rescue Exception => error                 # no such child
            raise if error.is_a? TimeoutStopError   # 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