Class: Opener::Daemons::Controller
- Inherits:
-
Object
- Object
- Opener::Daemons::Controller
- Defined in:
- lib/opener/daemons/controller.rb
Instance Attribute Summary collapse
-
#exec_path ⇒ Object
readonly
Returns the value of attribute exec_path.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #create_pid(pid) ⇒ Object
- #determine_name(name) ⇒ Object
- #get_name_from_exec_path ⇒ Object
- #get_pid ⇒ Object
- #identify(string) ⇒ Object
-
#initialize(options = {}) ⇒ Controller
constructor
A new instance of Controller.
- #options ⇒ Object
- #pid_path ⇒ Object
- #process_exists? ⇒ Boolean
- #read_commandline ⇒ Object
- #remove_pidfile ⇒ Object
- #start ⇒ Object
- #start_foreground ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Controller
Returns a new instance of Controller.
16 17 18 19 20 |
# File 'lib/opener/daemons/controller.rb', line 16 def initialize(={}) @exec_path = .fetch(:exec_path) @name = determine_name([:name]) read_commandline end |
Instance Attribute Details
#exec_path ⇒ Object (readonly)
Returns the value of attribute exec_path.
14 15 16 |
# File 'lib/opener/daemons/controller.rb', line 14 def exec_path @exec_path end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/opener/daemons/controller.rb', line 14 def name @name end |
Instance Method Details
#create_pid(pid) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/opener/daemons/controller.rb', line 62 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 |
#determine_name(name) ⇒ Object
22 23 24 25 |
# File 'lib/opener/daemons/controller.rb', line 22 def determine_name(name) return identify(name) unless name.nil? get_name_from_exec_path end |
#get_name_from_exec_path ⇒ Object
27 28 29 |
# File 'lib/opener/daemons/controller.rb', line 27 def get_name_from_exec_path File.basename(exec_path, ".rb") end |
#get_pid ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/opener/daemons/controller.rb', line 74 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 STDOUT.puts "Info: Unable to open #{pid_path} for reading while checking for existing PID:\n\t" + "(#{e.class}) #{e.message}" end if pid return pid.to_i else return nil end end |
#identify(string) ⇒ Object
161 162 163 |
# File 'lib/opener/daemons/controller.rb', line 161 def identify(string) string.gsub(/\W/,"-").gsub("--","-") end |
#options ⇒ Object
44 45 46 47 48 |
# File 'lib/opener/daemons/controller.rb', line 44 def return if args = ARGV.dup = Opener::Daemons::OptParser.pre_parse!(args) end |
#pid_path ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/opener/daemons/controller.rb', line 51 def pid_path return @pid_path unless @pid_path.nil? @pid_path = if [:pid] File.([:pid]) elsif [:pidpath] File.(File.join([:pidpath], "#{name}.pid")) else "/var/run/#{File.basename($0, ".rb")}.pid" end end |
#process_exists? ⇒ Boolean
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/opener/daemons/controller.rb', line 104 def process_exists? begin pid = get_pid return false unless pid Process.kill(0, pid) true rescue Errno::ESRCH, TypeError # "PID is NOT running or is zombied false rescue Errno::EPERM STDERR.puts "No permission to query #{pid}!"; false rescue => e STDERR.puts "Error: Unable to determine status for pid: #{pid}.\n\t" + "(#{e.class}) #{e.message}" false end end |
#read_commandline ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/opener/daemons/controller.rb', line 31 def read_commandline if ARGV[0] == 'start' start elsif ARGV[0] == 'stop' stop elsif ARGV[0] == 'restart' stop start else start_foreground end end |
#remove_pidfile ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/opener/daemons/controller.rb', line 94 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 |
#start ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/opener/daemons/controller.rb', line 139 def start if process_exists? STDERR.puts "Error: The process #{name} already running. Restarting the process" stop end STDOUT.puts "Starting DAEMON" pid = Spoon.spawnp exec_path, *ARGV STDOUT.puts "Started DAEMON" create_pid(pid) begin Process.setsid rescue Errno::EPERM => e STDERR.puts "Process.setsid not permitted on this platform, not critical. Continuing normal operations.\n\t (#{e.class}) #{e.message}" end File::umask(0) end |
#start_foreground ⇒ Object
157 158 159 |
# File 'lib/opener/daemons/controller.rb', line 157 def start_foreground exec [exec_path, ARGV].flatten.join(" ") end |
#stop ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/opener/daemons/controller.rb', line 122 def stop begin pid = get_pid STDOUT.puts "Stopping pid: #{pid}" while true do Process.kill("TERM", pid) Process.wait(pid) sleep(0.1) end rescue Errno::ESRCH # 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 |