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

#ensure_path_exits(file_name) ⇒ Object



23
24
25
26
# File 'lib/landrush/util/process_helper.rb', line 23

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

#process_status(file) ⇒ Object



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

def process_status(file)
  running?(file) ? :running : :stopped
end

#read_pid(file) ⇒ Object



12
13
14
15
16
17
# File 'lib/landrush/util/process_helper.rb', line 12

def read_pid(file)
  file.rewind
  file.read.to_i
rescue StandardError
  nil
end

#terminate_process(file) ⇒ Object



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

def terminate_process(file)
  pid = read_pid(file)

  # Kill/Term loop - if the daemon didn't die easily, shoot
  # it a few more times.
  attempts = 5
  while running?(file) && 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, file) ⇒ Object



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

def write_pid(pid, file)
  file.rewind
  file.truncate(0)
  file.write pid
  file.flush
end