2021-11-18 01:10:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module ProcessManagement
|
|
|
|
# Traps the given signals and yields the block whenever these signals are
|
|
|
|
# received.
|
|
|
|
#
|
|
|
|
# The block is passed the name of the signal.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# trap_signals(%i(HUP TERM)) do |signal|
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
def self.trap_signals(signals)
|
|
|
|
signals.each do |signal|
|
|
|
|
trap(signal) do
|
|
|
|
yield signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-15 19:15:50 -05:00
|
|
|
# Traps the given signals with the given command.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# modify_signals(%i(HUP TERM), 'DEFAULT')
|
|
|
|
def self.modify_signals(signals, command)
|
|
|
|
signals.each { |signal| trap(signal, command) }
|
2021-11-18 01:10:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.signal(pid, signal)
|
|
|
|
Process.kill(signal, pid)
|
|
|
|
true
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.signal_processes(pids, signal)
|
|
|
|
pids.each { |pid| signal(pid, signal) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Waits for the given process to complete using a separate thread.
|
|
|
|
def self.wait_async(pid)
|
|
|
|
Thread.new do
|
|
|
|
Process.wait(pid) rescue Errno::ECHILD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if all the processes are alive.
|
|
|
|
def self.all_alive?(pids)
|
|
|
|
pids.each do |pid|
|
|
|
|
return false unless process_alive?(pid)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.any_alive?(pids)
|
|
|
|
pids_alive(pids).any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pids_alive(pids)
|
|
|
|
pids.select { |pid| process_alive?(pid) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.process_alive?(pid)
|
2021-12-08 10:13:43 -05:00
|
|
|
return false if pid.nil?
|
|
|
|
|
2021-11-18 01:10:36 -05:00
|
|
|
# Signal 0 tests whether the process exists and we have access to send signals
|
|
|
|
# but is otherwise a noop (doesn't actually send a signal to the process)
|
|
|
|
signal(pid, 0)
|
|
|
|
end
|
|
|
|
|
2021-12-08 10:13:43 -05:00
|
|
|
def self.process_died?(pid)
|
|
|
|
!process_alive?(pid)
|
|
|
|
end
|
|
|
|
|
2021-11-18 01:10:36 -05:00
|
|
|
def self.write_pid(path)
|
|
|
|
File.open(path, 'w') do |handle|
|
|
|
|
handle.write(Process.pid.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|