Commits

Benjamin Smith  committed da3efc0

Add common shutdown handler. Check to see if nginx is running when we start, and at every main loop iteration. If it's not running, shut down.

  • Participants
  • Parent commits e0b5d19

Comments (0)

Files changed (1)

File nginxmgr/manager.py

 import threading
 import subprocess
 from SocketServer import ThreadingMixIn
-from Queue import Queue
+from Queue import Queue, Empty
 from SimpleXMLRPCServer import SimpleXMLRPCServer
 from nginxmgr.config import NginxConfig
 
 try:
     from configobj import ConfigObj
 except ImportError:
-    sys.exit('Please install the ConfigObj package from pypi')
+    self.shutdown('Please install the ConfigObj package from pypi')
 
 
 class ThreadedXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):
             if os.path.exists(config_file):
                 pass
         except IOError:
-            sys.exit('Config file does not exist: %s' % config_file, 1)
+            self.shutdown('Config file does not exist: %s' % config_file, 1)
 
         configobj = ConfigObj(config_file)
         return configobj
         """ Check to see if possibly running
         """
         if os.path.exists(self.pid_file):
-            sys.exit('%s exists, already running?' % self.pid_file)
+            self.shutdown('%s exists, already running?' % self.pid_file)
 
     def check_if_nginx_running(self):
         """ Check to make sure nginx is running, if not, don't start
         """ 
+        config_core = self.config.get('core')
         nginx_pid_file = config_core.get('ngx_pid_file')
         if not os.path.exists(nginx_pid_file):
-            sys.exit('%s does not exist.  Is nginx running?' % nginx_pid_file)
+            self.shutdown('%s does not exist.  Is nginx running?' % nginx_pid_file)
 
         nginx_pid = open(nginx_pid_file).read()
+        nginx_pid = nginx_pid.strip()
 
         if nginx_pid == '':
             nginx_pid = self.get_nginx_pid_from_ps()
 
         if not self.check_nginx_pid(nginx_pid):
-            sys.exit('Nginx process %s does not appear to be running!' % nginx_pid)
+            self.shutdown('Nginx process %s does not appear to be running!' % nginx_pid)
 
     def check_nginx_pid(self, nginx_pid):
         """ Check to make sure pid is active
 
         get_pid_stdout_val, get_pid_stderr_val = get_pid_process.communicate()
 
-        if get_pid_stdout_value.strip() == 'nginx':
+        if get_pid_stdout_val.strip() == 'nginx':
             return True
         else:
             return False
 
         get_pid_stdout_val, get_pid_stderr_val = get_pid_process.communicate()
 
-        if get_pid_stdout_value.strip() != '':
-            return True
+        if get_pid_stdout_val.strip() != '':
+            return get_pid_stdout_val.strip() 
         else:
             return False
- 
+
     def set_log_level(self):
         """ Set log level based on config values
         """
             except KeyError, exc_info:
                 self.logging.error('No upstreams matching config %s: %s'
                                       % (value.get('upstream'), self.upstreams))
-                sys.exit('Exception encountered: %s' % exc_info)
+                shutdown('Exception encountered: %s' % exc_info)
 
         self.logging.debug('we have %d healthchecks registered' % len(self.health_checks))
         self.logging.debug('health check setup, starting them up')
         if sig == 1:
             self.reload_everything()
         if sig in (3, 6, 15):
+            self.shutdown('Exiting on signal %d' % sig)
+
+    def shutdown(self, message):
+        # It's possible the queue hasn't been intialized yet
+        try:
             self.shutdown_worker_threads()
+        except Empty:
+            pass
+        # It's possible the pid file doesn't exist yet
+        try:
             os.remove(self.pid_file)
-            sys.exit('Exiting on signal %d' % sig)
+        except OSError:
+            pass
+        sys.exit(message)
 
     def daemonize(self):
         """ Daemonize our process
         """
         while True:
             try:
+                self.check_if_nginx_running()
                 self.logging.info('-- tick --')
                 self.logging.debug('checking upstreams list')
                 self.logging.info('%d checks running in %d threads'