]> git.ipfire.org Git - collecty.git/commitdiff
Get rid of python-daemon. Properly handle signals.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Aug 2012 14:23:48 +0000 (14:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Aug 2012 14:23:48 +0000 (14:23 +0000)
collecty/__init__.py
collecty/plugins/base.py
collectyd

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()
index 183e4c5b8e00cefa0d65c7d817a5ba4fbcb0827c..fb05bab6be318a2e96019cb0ec59627cb4c1cdc3 100644 (file)
 import logging
 import os
 import rrdtool
+import threading
 import time
 
-from threading import Thread
-
 from ..constants import *
 from ..i18n import _
 
-class Plugin(Thread):
+class Plugin(threading.Thread):
        # The name of this plugin.
        name = None
 
@@ -43,7 +42,9 @@ class Plugin(Thread):
        default_interval = 60
 
        def __init__(self, collecty, **kwargs):
-               Thread.__init__(self)
+               threading.Thread.__init__(self, name=self.description)
+               self.daemon = True
+
                self.collecty = collecty
 
                # Check if this plugin was configured correctly.
@@ -134,7 +135,7 @@ class Plugin(Thread):
                if not self.data:
                        return
 
-               self.collecty.debug(_("Saving data from %s...") % self)
+               self.log.debug(_("Submitting data to database. %d entries.") % len(self.data))
                rrdtool.update(self.file, *self.data)
                self.data = []
 
index 38bfe7e6041363c322a4847c181c5172ae18f884..ae0f6080acaf72d6ba6347965819656a145c7268 100755 (executable)
--- a/collectyd
+++ b/collectyd
 #                                                                             #
 ###############################################################################
 
-import os
-import sys
-
-import daemon
 import optparse
+import sys
 
 import collecty
 
-c = collecty.Collecty()
-
-# Parse command line options
+# Parse command line options.
 op = optparse.OptionParser(usage="usage: %prog [options] <configfile1> ... <configfileN>")
 op.add_option("-d", "--daemon", action="store_true", default=False,
                help="Run as a daemon in background.")
 (options, configfiles) = op.parse_args()
 
-if configfiles:
-       for file in configfiles:
-               c.read_config(file)
-else:
-       # Load default config file
-       c.read_config("/etc/collecty/collecty.conf")
-
-if not c.instances:
-       print >>sys.stderr, "Error: No instances were configured."
-       sys.exit(1)
+# Initialize the application.
+c = collecty.Collecty()
 
-if options.daemon:
-       with daemon.DaemonContext(stdout=sys.stdout, stderr=sys.stderr):
-               c.run()
-else:
-       c.run()
+# Run.
+c.run()
 
+sys.exit(0)