class Rsense::Client::Runner

Constants

APPNAME
EXEC
OUT_PATH
PID_PATH

TODO: Make this configurable w/out ENV variables

WORK_PATH

Attributes

args[RW]

Public Class Methods

get_pid() click to toggle source
# File lib/rsense/client/runner.rb, line 22
def self.get_pid
  new.get_pid
end
new(args={}) click to toggle source
# File lib/rsense/client/runner.rb, line 17
def initialize(args={})
  ensure_paths_exist(PID_PATH, OUT_PATH)
  @args = args
end

Public Instance Methods

create_pid(pid) click to toggle source
# File lib/rsense/client/runner.rb, line 26
def create_pid(pid)
  begin
    open(pid_path, 'w') do |f|
      f.puts pid
    end
  rescue => e
    STDERR.puts "Error: Unable to open #{pid_path} for writing:\n\t" +
        "(#{e.class}) #{e.message}"
    exit!
  end
end
ensure_paths_exist(*paths) click to toggle source
# File lib/rsense/client/runner.rb, line 130
def ensure_paths_exist(*paths)
  paths.each do |path|
    FileUtils.mkdir_p(path) unless File.directory?(path)
  end
end
get_pid() click to toggle source
# File lib/rsense/client/runner.rb, line 38
def get_pid
  pid = false
  begin
    open(pid_path, 'r') do |f|
      pid = f.readline
      pid = pid.to_s.gsub(/[^0-9]/,'')
    end
  rescue => e
    STDERR.puts "Error: Unable to open #{pid_path} for reading:\n\t" +
        "(#{e.class}) #{e.message}"
    exit
  end

  pid.to_i
end
out_path() click to toggle source
# File lib/rsense/client/runner.rb, line 126
def out_path
  File.join(OUT_PATH, "rsense.log")
end
pid_path() click to toggle source
# File lib/rsense/client/runner.rb, line 122
def pid_path
  File.join(PID_PATH, "rsense.pid")
end
process_exists?() click to toggle source
# File lib/rsense/client/runner.rb, line 64
def process_exists?
  begin
    pid = get_pid
    return false unless pid
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH, TypeError
    STDERR.puts "PID #{pid} is NOT running or is zombied!";
    false
  rescue Errno::EPERM
    STDERR.puts "No permission to query #{pid}!";
  rescue => e
    STDERR.puts "(#{e.class}) #{e.message}:\n\t" +
      "Unable to determine status for #{pid}."
    false
  end
end
remove_pidfile() click to toggle source
# File lib/rsense/client/runner.rb, line 54
def remove_pidfile
  begin
    File.unlink(pid_path)
  rescue => e
    STDERR.puts "ERROR: Unable to unlink #{path}:\n\t" +
      "(#{e.class}) #{e.message}"
    exit
  end
end
restart() click to toggle source
# File lib/rsense/client/runner.rb, line 102
def restart
  if process_exists?
    STDERR.puts "The process is already running. Restarting the process"
    stop
  end
  start
end
start() click to toggle source
# File lib/rsense/client/runner.rb, line 110
def start
  Dir::chdir(WORK_PATH)
  file_actions = Spoon::FileActions.new
  file_actions.close(1)
  file_actions.open(1, out_path, File::WRONLY | File::TRUNC | File::CREAT, 0600)
  file_actions.close(2)
  file_actions.open(2, out_path, File::WRONLY | File::TRUNC | File::CREAT, 0600)
  spawn_attr =  Spoon::SpawnAttributes.new
  pid = Spoon.posix_spawn EXEC, file_actions, spawn_attr, @args
  create_pid(pid)
end
stop() click to toggle source
# File lib/rsense/client/runner.rb, line 82
def stop
  begin
    pid = get_pid
    STDERR.puts "pid : #{pid}"
    while true do
      Process.kill("TERM", pid)
      Process.wait(pid)
      sleep(2.0)
      Process.kill("KILL", pid)
      sleep(0.1)
    end
    puts "here"
  rescue Errno::ESRCH, Errno::ECHILD # no more process to kill
    remove_pidfile
    STDOUT.puts 'Stopped the process'
  rescue => e
    STDERR.puts "unable to terminate process: (#{e.class}) #{e.message}"
  end
end