]> git.ipfire.org Git - collecty.git/blobdiff - collecty/__init__.py
Get rid of python-daemon. Properly handle signals.
[collecty.git] / collecty / __init__.py
index 5fdcdd198e37fa73ee4d6f50eba870fc03621671..158581310a428dbd89051196ebdb8eab936169de 100644 (file)
 #                                                                             #
 ###############################################################################
 
-
 import signal
+import time
 
 import ConfigParser as configparser
 
 import plugins
 
+from i18n import _
+
 # Initialize logging.
 import logging
 log = logging.getLogger("collecty")
@@ -67,12 +69,31 @@ class Collecty(object):
                        self.instances.append(i)
 
        def run(self):
-               signal.signal(signal.SIGTERM, lambda *args: self.shutdown())
+               # Register signal handlers.
+               self.register_signal_handler()
 
+               # Start all plugin instances.
                for i in self.instances:
                        i.start()
 
+               # As long as at least one thread is alive, the main process
+               # is in a while loop.
+               while any([i.isAlive() for i in self.instances]):
+                       time.sleep(0.5)
+
+               log.debug(_("No thread running. Exiting main thread."))
+
        def shutdown(self):
+               log.debug(_("Received shutdown signal"))
+
+               # Propagating shutdown to all threads.
                for i in self.instances:
-                       self.debug("Stopping %s..." % i)
                        i.shutdown()
+
+       def register_signal_handler(self):
+               for s in (signal.SIGTERM, signal.SIGINT):
+                       signal.signal(s, self.signal_handler)
+
+       def signal_handler(self, *args, **kwargs):
+               # Shutdown this application.
+               self.shutdown()