Module: Landrush::Util::ProcessHelper

Included in:
Server
Defined in:
lib/landrush/util/process_helper.rb

Overview

A module containing helper classes for dealing with pid files

Instance Method Summary collapse

Instance Method Details

#delete_pid_file(pid_file) ⇒ Object



16
17
18
# File 'lib/landrush/util/process_helper.rb', line 16

def delete_pid_file(pid_file)
  FileUtils.rm(pid_file) if File.exist? pid_file
end

#ensure_path_exits(file_name) ⇒ Object



25
26
27
28
# File 'lib/landrush/util/process_helper.rb', line 25

def ensure_path_exits(file_name)
  dirname = File.dirname(file_name)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
end

#process_status(pid_file) ⇒ Object



20
21
22
23
# File 'lib/landrush/util/process_helper.rb', line 20

def process_status(pid_file)
  return running? ? :running : :unknown if File.exist? pid_file
  :stopped
end

#read_pid(pid_file) ⇒ Object



10
11
12
13
14
# File 'lib/landrush/util/process_helper.rb', line 10

def read_pid(pid_file)
  IO.read(pid_file).to_i
rescue
  nil
end

#terminate_process(pid) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/landrush/util/process_helper.rb', line 30

def terminate_process(pid)
  # Kill/Term loop - if the daemon didn't die easily, shoot
  # it a few more times.
  attempts = 5
  while running? && attempts > 0
    sig = (attempts >= 2) ? 'KILL' : 'TERM'

    puts "Sending #{sig} to process #{pid}..."
    Process.kill(sig, pid)

    attempts -= 1
    sleep 1
  end
end

#write_pid(pid, pid_file) ⇒ Object



5
6
7
8
# File 'lib/landrush/util/process_helper.rb', line 5

def write_pid(pid, pid_file)
  ensure_path_exits(pid_file)
  File.open(pid_file, 'w') { |f| f << pid.to_s }
end