module Kajiki::Handler

Public Instance Methods

change_privileges() click to toggle source

Change process UID and GID.

# File lib/kajiki/handler.rb, line 53
def change_privileges
  Process.egid = @opts[:group] if @opts[:group_given]
  Process.euid = @opts[:user] if @opts[:user_given]
end
check_existing_pid() click to toggle source

Check if process exists then fail, otherwise clean up. @return [Boolean] `false` if no PID file exists, `true` if it cleaned up.

# File lib/kajiki/handler.rb, line 7
def check_existing_pid
  return false unless pid_file_exists?
  pid = read_pid
  fail 'Existing process found.' if pid > 0 && pid_exists?(pid)
  delete_pid
end
delete_pid() click to toggle source

Delete PID file if it exists. @return [Boolean] `false` if nothing done, `true` if file was deleted.

# File lib/kajiki/handler.rb, line 44
def delete_pid
  if @opts[:pid_given] && File.exists?(@opts[:pid])
    File.delete(@opts[:pid]) 
    return true
  end
  return false
end
pid_exists?(pid) click to toggle source

Check if process exists. @param pid [Fixnum] @return [Boolean]

# File lib/kajiki/handler.rb, line 17
def pid_exists?(pid)
  Process.kill(0, pid)
  return true
rescue Errno::ESRCH
  return false
end
pid_file_exists?() click to toggle source

Check if PID file exists. @return [Boolean]

# File lib/kajiki/handler.rb, line 26
def pid_file_exists?
  return false unless @opts[:pid_given]
  File.exists?(@opts[:pid])
end
read_pid() click to toggle source

Read PID from file. @return [Fixnum, nil] PID; if `0`, it should be ignored.

# File lib/kajiki/handler.rb, line 38
def read_pid
  IO.read(@opts[:pid]).to_i if @opts[:pid_given]
end
redirect_outputs() click to toggle source

Redirect outputs.

# File lib/kajiki/handler.rb, line 59
def redirect_outputs
  if @opts[:error_given]
    $stderr.reopen(@opts[:error], 'a') 
    $stderr.sync = true
  end
  if @opts[:log_given]
    $stdout.reopen(@opts[:log], 'a') 
    $stdout.sync = true
  end
end
trap_default_signals() click to toggle source

Trap common signals as default.

# File lib/kajiki/handler.rb, line 71
def trap_default_signals
  Signal.trap('INT') do
    puts 'Interrupted. Terminating process...'
    exit
  end
  Signal.trap('HUP') do
    puts 'SIGHUP - Terminating process...'
    exit
  end
  Signal.trap('TERM') do
    puts 'SIGTERM - Terminating process...'
    exit
  end
end
write_pid() click to toggle source

Write PID to file.

# File lib/kajiki/handler.rb, line 32
def write_pid
  IO.write(@opts[:pid], Process.pid) if @opts[:pid_given]
end