Class: Rsense::Client::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rsense/client/runner.rb

Constant Summary collapse

APPNAME =
'rsense'
WORK_PATH =
Dir.pwd
PID_PATH =

TODO: Make this configurable w/out ENV variables

ENV['RSENSE_PID'] || '/tmp'
OUT_PATH =
ENV['RSENSE_LOG'] || '/tmp'
EXEC =
"/usr/bin/env"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Runner

Returns a new instance of Runner.



17
18
19
20
# File 'lib/rsense/client/runner.rb', line 17

def initialize(args={})
  ensure_paths_exist(PID_PATH, OUT_PATH)
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



8
9
10
# File 'lib/rsense/client/runner.rb', line 8

def args
  @args
end

Class Method Details

.get_pidObject



22
23
24
# File 'lib/rsense/client/runner.rb', line 22

def self.get_pid
  new.get_pid
end

Instance Method Details

#create_pid(pid) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



130
131
132
133
134
# 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_pidObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_pathObject



126
127
128
# File 'lib/rsense/client/runner.rb', line 126

def out_path
  File.join(OUT_PATH, "rsense.log")
end

#pid_pathObject



122
123
124
# File 'lib/rsense/client/runner.rb', line 122

def pid_path
  File.join(PID_PATH, "rsense.pid")
end

#process_exists?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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_pidfileObject



54
55
56
57
58
59
60
61
62
# 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

#restartObject



102
103
104
105
106
107
108
# 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

#startObject



110
111
112
113
114
115
116
117
118
119
120
# 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

#stopObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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