From: Michael Tremer Date: Sat, 4 Aug 2012 14:23:48 +0000 (+0000) Subject: Get rid of python-daemon. Properly handle signals. X-Git-Tag: 0.0.2~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49ce926e3c7f810c288aaf4be7ec7402eec285df;p=collecty.git Get rid of python-daemon. Properly handle signals. --- diff --git a/collecty/__init__.py b/collecty/__init__.py index 5fdcdd1..1585813 100644 --- a/collecty/__init__.py +++ b/collecty/__init__.py @@ -19,13 +19,15 @@ # # ############################################################################### - 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() diff --git a/collecty/plugins/base.py b/collecty/plugins/base.py index 183e4c5..fb05bab 100644 --- a/collecty/plugins/base.py +++ b/collecty/plugins/base.py @@ -22,14 +22,13 @@ 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 = [] diff --git a/collectyd b/collectyd index 38bfe7e..ae0f608 100755 --- a/collectyd +++ b/collectyd @@ -19,36 +19,21 @@ # # ############################################################################### -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] ... ") 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)