class TorManager::ProcessHelper
Public Class Methods
kill_process(pids)
click to toggle source
# File lib/tormanager/process_helper.rb, line 15 def kill_process pids to_array(pids).each do |pid| try_to_kill pid: Integer(pid), attempts: 5 end end
port_is_open?(port)
click to toggle source
# File lib/tormanager/process_helper.rb, line 33 def port_is_open? port begin server = TCPServer.new('127.0.0.1', port) server.close return true rescue Errno::EADDRINUSE; return false end end
process_pid_running?(pid)
click to toggle source
# File lib/tormanager/process_helper.rb, line 21 def process_pid_running? pid begin return false if pid.to_s == ''.freeze ipid = Integer(pid) return false if ipid <= 0 Process.kill(0, ipid) return true rescue return false end end
query_process(query)
click to toggle source
# File lib/tormanager/process_helper.rb, line 8 def query_process query return [] unless query query_process_bash_cmd(query).split("\n").map{ |query_output_line| get_pid_from_query_process_output_line(query_output_line) }.compact end
Private Class Methods
get_pid_from_query_process_output_line(query_output_line)
click to toggle source
# File lib/tormanager/process_helper.rb, line 55 def get_pid_from_query_process_output_line query_output_line output_parts = query_output_line.gsub(/\s\s+/, ' ').strip.split output_parts.size >= 3 && output_parts[1].to_i > 0 ? output_parts[1].to_i : nil end
query_grep_pipe_chain(query)
click to toggle source
# File lib/tormanager/process_helper.rb, line 49 def query_grep_pipe_chain query to_array(query) .map{|q| "grep '#{q}'"} .join(' | ') end
query_process_bash_cmd(query)
click to toggle source
# File lib/tormanager/process_helper.rb, line 45 def query_process_bash_cmd query `ps -ef | #{query_grep_pipe_chain(query)} | grep -v grep` end
to_array(v)
click to toggle source
# File lib/tormanager/process_helper.rb, line 61 def to_array v (v.kind_of?(Array) ? v : [v]) end
try_to_kill(params={})
click to toggle source
# File lib/tormanager/process_helper.rb, line 65 def try_to_kill params={} pid = params.fetch(:pid, nil) return unless pid && process_pid_running?(pid) params.fetch(:attempts, 5).times do |i| i < 3 ? Process.kill('TERM', pid) : Process.kill('KILL', pid) sleep 0.5 break unless process_pid_running? pid raise CannotKillProcess, "Couldnt kill pid: #{pid}" if i >= 4 end end