Class: Percy::ProcessHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/percy/process_helpers.rb

Constant Summary collapse

DEFAULT_TERM_GRACE_SECONDS =
10

Class Method Summary collapse

Class Method Details

.gracefully_kill(pid, grace_period_seconds: DEFAULT_TERM_GRACE_SECONDS) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/percy/process_helpers.rb', line 7

def self.gracefully_kill(pid, grace_period_seconds: DEFAULT_TERM_GRACE_SECONDS)
  begin
    Process.kill('TERM', pid)
    Timeout.timeout(grace_period_seconds) do
      Process.wait(pid)
    end
  rescue Errno::ESRCH
    # No such process.
    return false
  rescue Errno::ECHILD
    # Status has already been collected, perhaps by a Process.detach thread.
    return false
  rescue Timeout::Error
    begin
      Process.kill('KILL', pid)
    rescue Errno::ESRCH
      # If the process has already ended, suppress any additional errors
      return false
    end
    # Collect status so it doesn't stick around as zombie process.
    Process.wait(pid, Process::WNOHANG)
  end
  true
end