lib/bluepill/process.rb in bluepill-0.0.6 vs lib/bluepill/process.rb in bluepill-0.0.7

- old
+ new

@@ -228,30 +228,35 @@ def start_process logger.warning "Executing start command: #{start_command}" if self.daemonize? - starter = lambda { drop_privileges; ::Kernel.exec(start_command) } - child_pid = Daemonize.call_as_daemon(starter) + child_pid = System.daemonize(start_command, :uid => self.uid, :gid => self.gid) File.open(pid_file, "w") {|f| f.write(child_pid)} + else # This is a self-daemonizing process - unless System.execute_blocking(start_command) - logger.warning "Start command execution returned non-zero exit code" + result = System.execute_blocking(start_command, :uid => self.uid, :gid => self.gid, :logger => logger) + + unless result[:exit_code].zero? + logger.warning "Start command execution returned non-zero exit code:" + logger.warning result.inspect end end self.skip_ticks_for(start_grace_time) end def stop_process if stop_command cmd = stop_command.to_s.gsub("{{PID}}", actual_pid.to_s) logger.warning "Executing stop command: #{cmd}" - - unless System.execute_blocking(cmd) - logger.warning "Stop command execution returned non-zero exit code" + + result = System.execute_blocking(cmd, :uid => self.uid, :gid => self.gid) + unless result[:exit_code].zero? + logger.warning "Stop command execution returned non-zero exit code:" + logger.warning result.inspect end else logger.warning "Executing default stop command. Sending TERM signal to #{actual_pid}" signal_process("TERM") @@ -263,12 +268,14 @@ def restart_process if restart_command logger.warning "Executing restart command: #{restart_command}" - unless System.execute_blocking(restart_command) - logger.warning "Restart command execution returned non-zero exit code" + result = System.execute_blocking(restart_command, :uid => self.uid, :gid => self.gid) + unless result[:exit_code].zero? + logger.warning "Restart command execution returned non-zero exit code:" + logger.warning result.inspect end self.skip_ticks_for(restart_grace_time) else logger.warning "No restart_command specified. Must stop and start to restart" @@ -292,11 +299,18 @@ false end def actual_pid @actual_pid ||= begin - File.read(pid_file).to_i if pid_file && File.exists?(pid_file) + if pid_file + if File.exists?(pid_file) + File.read(pid_file).to_i + else + logger.warning("pid_file #{pid_file} does not exist or cannot be read") + nil + end + end end end def actual_pid=(pid) @actual_pid = pid @@ -308,27 +322,10 @@ def unlink_pid File.unlink(pid_file) if pid_file && File.exists?(pid_file) end - def drop_privileges - begin - require 'etc' - - uid_num = Etc.getpwnam(self.uid).uid if self.uid - gid_num = Etc.getgrnam(self.gid).gid if self.gid - - ::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 => e - # TODO: log exceptions elsewhere - File.open("/tmp/exception.log", "w+"){|f| puts e} - end - end - - - # Internal State Methods + # Internal State Methods def skip_ticks_for(seconds) # TODO: should this be addative or longest wins? # i.e. if two calls for skip_ticks_for come in for 5 and 10, should it skip for 10 or 15? self.skip_ticks_until = (self.skip_ticks_until || Time.now.to_i) + seconds.to_i end \ No newline at end of file