Class: Rsense::Client::Runner
- Inherits:
-
Object
- Object
- Rsense::Client::Runner
- Defined in:
- lib/rsense/client/runner.rb
Constant Summary collapse
- APPNAME =
'rsense'- WORK_PATH =
Dir.pwd
- PID_PATH =
'/tmp/exec.pid'- OUT_PATH =
'/tmp/rsense.log'- EXEC =
"/usr/bin/env"
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
Instance Method Summary collapse
- #create_pid(pid) ⇒ Object
- #get_pid ⇒ Object
-
#initialize(args) ⇒ Runner
constructor
A new instance of Runner.
- #process_exists? ⇒ Boolean
- #remove_pidfile ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(args) ⇒ Runner
Returns a new instance of Runner.
15 16 17 |
# File 'lib/rsense/client/runner.rb', line 15 def initialize(args) @args = args end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
7 8 9 |
# File 'lib/rsense/client/runner.rb', line 7 def args @args end |
Instance Method Details
#create_pid(pid) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rsense/client/runner.rb', line 19 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 |
#get_pid ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rsense/client/runner.rb', line 31 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 |
#process_exists? ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rsense/client/runner.rb', line 57 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 ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/rsense/client/runner.rb', line 47 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 ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/rsense/client/runner.rb', line 95 def restart if process_exists? STDERR.puts "The process is already running. Restarting the process" stop end start end |
#start ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rsense/client/runner.rb', line 103 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 ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rsense/client/runner.rb', line 75 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 |