Class: God::Process
- Inherits:
-
Object
- Object
- God::Process
- Defined in:
- lib/god/process.rb
Constant Summary collapse
- WRITES_PID =
[:start, :restart]
Instance Attribute Summary collapse
-
#chroot ⇒ Object
Returns the value of attribute chroot.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#env ⇒ Object
Returns the value of attribute env.
-
#err_log ⇒ Object
Returns the value of attribute err_log.
-
#err_log_cmd ⇒ Object
Returns the value of attribute err_log_cmd.
-
#gid ⇒ Object
Returns the value of attribute gid.
-
#log ⇒ Object
Returns the value of attribute log.
-
#log_cmd ⇒ Object
Returns the value of attribute log_cmd.
-
#name ⇒ Object
Returns the value of attribute name.
-
#restart ⇒ Object
Returns the value of attribute restart.
-
#start ⇒ Object
Returns the value of attribute start.
-
#stop ⇒ Object
Returns the value of attribute stop.
-
#stop_signal ⇒ Object
Returns the value of attribute stop_signal.
-
#stop_timeout ⇒ Object
Returns the value of attribute stop_timeout.
-
#uid ⇒ Object
Returns the value of attribute uid.
-
#unix_socket ⇒ Object
Returns the value of attribute unix_socket.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #call_action(action) ⇒ Object
- #default_pid_file ⇒ Object
-
#ensure_stop ⇒ Object
Ensure that a stop command actually stops the process.
- #file_writable?(file) ⇒ Boolean
-
#initialize ⇒ Process
constructor
A new instance of Process.
-
#pid ⇒ Object
Fetch the PID from pid_file.
- #pid_file ⇒ Object
-
#pid_file=(value) ⇒ Object
DON’T USE THIS INTERNALLY.
- #restart! ⇒ Object
-
#signal(sig) ⇒ Object
Send the given signal to this process.
-
#spawn(command) ⇒ Object
Fork/exec the given command, returns immediately
command
is the String containing the shell command. - #start! ⇒ Object
- #stop! ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Process
Returns a new instance of Process.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/god/process.rb', line 9 def initialize self.log = '/dev/null' @pid_file = nil @tracking_pid = true @user_log = false @pid = nil @unix_socket = nil @log_cmd = nil @stop_timeout = God::STOP_TIMEOUT_DEFAULT @stop_signal = God::STOP_SIGNAL_DEFAULT end |
Instance Attribute Details
#chroot ⇒ Object
Returns the value of attribute chroot.
5 6 7 |
# File 'lib/god/process.rb', line 5 def chroot @chroot end |
#dir ⇒ Object
Returns the value of attribute dir.
5 6 7 |
# File 'lib/god/process.rb', line 5 def dir @dir end |
#env ⇒ Object
Returns the value of attribute env.
5 6 7 |
# File 'lib/god/process.rb', line 5 def env @env end |
#err_log ⇒ Object
Returns the value of attribute err_log.
5 6 7 |
# File 'lib/god/process.rb', line 5 def err_log @err_log end |
#err_log_cmd ⇒ Object
Returns the value of attribute err_log_cmd.
5 6 7 |
# File 'lib/god/process.rb', line 5 def err_log_cmd @err_log_cmd end |
#gid ⇒ Object
Returns the value of attribute gid.
5 6 7 |
# File 'lib/god/process.rb', line 5 def gid @gid end |
#log ⇒ Object
Returns the value of attribute log.
5 6 7 |
# File 'lib/god/process.rb', line 5 def log @log end |
#log_cmd ⇒ Object
Returns the value of attribute log_cmd.
5 6 7 |
# File 'lib/god/process.rb', line 5 def log_cmd @log_cmd end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/god/process.rb', line 5 def name @name end |
#restart ⇒ Object
Returns the value of attribute restart.
5 6 7 |
# File 'lib/god/process.rb', line 5 def restart @restart end |
#start ⇒ Object
Returns the value of attribute start.
5 6 7 |
# File 'lib/god/process.rb', line 5 def start @start end |
#stop ⇒ Object
Returns the value of attribute stop.
5 6 7 |
# File 'lib/god/process.rb', line 5 def stop @stop end |
#stop_signal ⇒ Object
Returns the value of attribute stop_signal.
5 6 7 |
# File 'lib/god/process.rb', line 5 def stop_signal @stop_signal end |
#stop_timeout ⇒ Object
Returns the value of attribute stop_timeout.
5 6 7 |
# File 'lib/god/process.rb', line 5 def stop_timeout @stop_timeout end |
#uid ⇒ Object
Returns the value of attribute uid.
5 6 7 |
# File 'lib/god/process.rb', line 5 def uid @uid end |
#unix_socket ⇒ Object
Returns the value of attribute unix_socket.
5 6 7 |
# File 'lib/god/process.rb', line 5 def unix_socket @unix_socket end |
Instance Method Details
#alive? ⇒ Boolean
22 23 24 25 26 27 28 |
# File 'lib/god/process.rb', line 22 def alive? if self.pid System::Process.new(self.pid).exists? else false end end |
#call_action(action) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/god/process.rb', line 200 def call_action(action) command = send(action) if action == :stop && command.nil? pid = self.pid name = self.name command = lambda do applog(self, :info, "#{self.name} stop: default lambda killer") ::Process.kill(@stop_signal, pid) rescue nil applog(self, :info, "#{self.name} sent SIG#{@stop_signal}") # Poll to see if it's dead @stop_timeout.times do begin ::Process.kill(0, pid) rescue Errno::ESRCH # It died. Good. applog(self, :info, "#{self.name} process stopped") return end sleep 1 end ::Process.kill('KILL', pid) rescue nil applog(self, :warn, "#{self.name} still alive after #{@stop_timeout}s; sent SIGKILL") end end if command.kind_of?(String) pid = nil if [:start, :restart].include?(action) && @tracking_pid # double fork god-daemonized processes # we don't want to wait for them to finish r, w = IO.pipe begin opid = fork do STDOUT.reopen(w) r.close pid = self.spawn(command) puts pid.to_s # send pid back to forker end ::Process.waitpid(opid, 0) w.close pid = r.gets.chomp ensure # make sure the file descriptors get closed no matter what r.close rescue nil w.close rescue nil end else # single fork self-daemonizing processes # we want to wait for them to finish pid = self.spawn(command) status = ::Process.waitpid2(pid, 0) exit_code = status[1] >> 8 if exit_code != 0 applog(self, :warn, "#{self.name} #{action} command exited with non-zero code = #{exit_code}") end ensure_stop if action == :stop end if @tracking_pid or (@pid_file.nil? and WRITES_PID.include?(action)) File.open(default_pid_file, 'w') do |f| f.write pid end @tracking_pid = true @pid_file = default_pid_file end elsif command.kind_of?(Proc) # lambda command command.call else raise NotImplementedError end end |
#default_pid_file ⇒ Object
196 197 198 |
# File 'lib/god/process.rb', line 196 def default_pid_file File.join(God.pid_file_directory, "#{self.name}.pid") end |
#ensure_stop ⇒ Object
Ensure that a stop command actually stops the process. Force kill if necessary.
Returns nothing
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/god/process.rb', line 331 def ensure_stop applog(self, :warn, "#{self.name} ensuring stop...") unless self.pid applog(self, :warn, "#{self.name} stop called but pid is uknown") return end # Poll to see if it's dead @stop_timeout.times do begin ::Process.kill(0, self.pid) rescue Errno::ESRCH # It died. Good. return end sleep 1 end # last resort ::Process.kill('KILL', self.pid) rescue nil applog(self, :warn, "#{self.name} still alive after #{@stop_timeout}s; sent SIGKILL") end |
#file_writable?(file) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/god/process.rb', line 30 def file_writable?(file) pid = fork do begin uid_num = Etc.getpwnam(self.uid).uid if self.uid gid_num = Etc.getgrnam(self.gid).gid if self.gid ::Dir.chroot(self.chroot) if self.chroot ::Process.groups = [gid_num] if self.gid ::Process::Sys.setgid(gid_num) if self.gid ::Process::Sys.setuid(uid_num) if self.uid rescue ArgumentError, Errno::EPERM, Errno::ENOENT exit(1) end File.writable?(file_in_chroot(file)) ? exit(0) : exit(1) end wpid, status = ::Process.waitpid2(pid) status.exitstatus == 0 ? true : false end |
#pid ⇒ Object
Fetch the PID from pid_file. If the pid_file does not exist, then use the PID from the last time it was read. If it has never been read, then return nil.
Returns Integer(pid) or nil
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/god/process.rb', line 163 def pid contents = File.read(self.pid_file).strip rescue '' real_pid = contents =~ /^\d+$/ ? contents.to_i : nil if real_pid @pid = real_pid real_pid else @pid end end |
#pid_file ⇒ Object
154 155 156 |
# File 'lib/god/process.rb', line 154 def pid_file @pid_file ||= default_pid_file end |
#pid_file=(value) ⇒ Object
DON’T USE THIS INTERNALLY. Use the instance variable. – Kev No really, trust me. Use the instance variable.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/god/process.rb', line 143 def pid_file=(value) # if value is nil, do the right thing if value @tracking_pid = false else @tracking_pid = true end @pid_file = value end |
#restart! ⇒ Object
192 193 194 |
# File 'lib/god/process.rb', line 192 def restart! call_action(:restart) end |
#signal(sig) ⇒ Object
Send the given signal to this process.
Returns nothing
178 179 180 181 182 |
# File 'lib/god/process.rb', line 178 def signal(sig) sig = sig.to_i if sig.to_i != 0 applog(self, :info, "#{self.name} sending signal '#{sig}' to pid #{self.pid}") ::Process.kill(sig, self.pid) rescue nil end |
#spawn(command) ⇒ Object
Fork/exec the given command, returns immediately
+command+ is the String containing the shell command
Returns nothing
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/god/process.rb', line 287 def spawn(command) fork do uid_num = Etc.getpwnam(self.uid).uid if self.uid gid_num = Etc.getgrnam(self.gid).gid if self.gid ::Dir.chroot(self.chroot) if self.chroot ::Process.setsid ::Process.groups = [gid_num] if self.gid ::Process::Sys.setgid(gid_num) if self.gid ::Process::Sys.setuid(uid_num) if self.uid self.dir ||= '/' Dir.chdir self.dir $0 = command STDIN.reopen "/dev/null" if self.log_cmd STDOUT.reopen IO.popen(self.log_cmd, "a") else STDOUT.reopen file_in_chroot(self.log), "a" end if err_log_cmd STDERR.reopen IO.popen(err_log_cmd, "a") elsif err_log && (log_cmd || err_log != log) STDERR.reopen file_in_chroot(err_log), "a" else STDERR.reopen STDOUT end # close any other file descriptors 3.upto(256){|fd| IO::new(fd).close rescue nil} if self.env && self.env.is_a?(Hash) self.env.each do |(key, value)| ENV[key] = value end end exec command unless command.empty? end end |
#start! ⇒ Object
184 185 186 |
# File 'lib/god/process.rb', line 184 def start! call_action(:start) end |
#stop! ⇒ Object
188 189 190 |
# File 'lib/god/process.rb', line 188 def stop! call_action(:stop) end |
#valid? ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/god/process.rb', line 51 def valid? # determine if we're tracking pid or not self.pid_file valid = true # a start command must be specified if self.start.nil? valid = false applog(self, :error, "No start command was specified") end # uid must exist if specified if self.uid begin Etc.getpwnam(self.uid) rescue ArgumentError valid = false applog(self, :error, "UID for '#{self.uid}' does not exist") end end # gid must exist if specified if self.gid begin Etc.getgrnam(self.gid) rescue ArgumentError valid = false applog(self, :error, "GID for '#{self.gid}' does not exist") end end # dir must exist and be a directory if specified if self.dir if !File.exist?(self.dir) valid = false applog(self, :error, "Specified directory '#{self.dir}' does not exist") elsif !File.directory?(self.dir) valid = false applog(self, :error, "Specified directory '#{self.dir}' is not a directory") end end # pid dir must exist if specified if !@tracking_pid && !File.exist?(File.dirname(self.pid_file)) valid = false applog(self, :error, "PID file directory '#{File.dirname(self.pid_file)}' does not exist") end # pid dir must be writable if specified if !@tracking_pid && File.exist?(File.dirname(self.pid_file)) && !file_writable?(File.dirname(self.pid_file)) valid = false applog(self, :error, "PID file directory '#{File.dirname(self.pid_file)}' is not writable by #{self.uid || Etc.getlogin}") end # log dir must exist if !File.exist?(File.dirname(self.log)) valid = false applog(self, :error, "Log directory '#{File.dirname(self.log)}' does not exist") end # log file or dir must be writable if File.exist?(self.log) unless file_writable?(self.log) valid = false applog(self, :error, "Log file '#{self.log}' exists but is not writable by #{self.uid || Etc.getlogin}") end else unless file_writable?(File.dirname(self.log)) valid = false applog(self, :error, "Log directory '#{File.dirname(self.log)}' is not writable by #{self.uid || Etc.getlogin}") end end # chroot directory must exist and have /dev/null in it if self.chroot if !File.directory?(self.chroot) valid = false applog(self, :error, "CHROOT directory '#{self.chroot}' does not exist") end if !File.exist?(File.join(self.chroot, '/dev/null')) valid = false applog(self, :error, "CHROOT directory '#{self.chroot}' does not contain '/dev/null'") end end valid end |