X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/aca5759480d286781835ef711da8c22fa2104452..af3261630a29ae3ee1f4c9a4eb574998b3e1a47b:/pymin/pymindaemon.py diff --git a/pymin/pymindaemon.py b/pymin/pymindaemon.py index ccb943d..067a727 100644 --- a/pymin/pymindaemon.py +++ b/pymin/pymindaemon.py @@ -10,11 +10,13 @@ command-line. import signal import socket +import logging ; log = logging.getLogger('pymin.pymindaemon') from pymin.dispatcher import handler from pymin import dispatcher from pymin import eventloop from pymin import serializer +from pymin import procman class PyminDaemon(eventloop.EventLoop): r"""PyminDaemon(root, bind_addr) -> PyminDaemon instance @@ -42,6 +44,7 @@ class PyminDaemon(eventloop.EventLoop): See PyminDaemon class documentation for more info. """ + log.debug(u'PyminDaemon(%r, %r, %r)', root, bind_addr, timer) # Timer timeout time self.timer = timer # Create and bind socket @@ -50,20 +53,25 @@ class PyminDaemon(eventloop.EventLoop): sock.bind(bind_addr) # Signal handling def quit(loop, signum): - print "Shuting down ..." + log.debug(u'PyminDaemon quit() handler: signal %r', signum) + log.info(u'Shutting down...') loop.stop() # tell main event loop to stop def reload_config(loop, signum): - print "Reloading configuration..." + log.debug(u'PyminDaemon reload_config() handler: signal %r', signum) + log.info(u'Reloading configuration...') # TODO iterate handlers list propagating reload action def timer(loop, signum): loop.handle_timer() signal.alarm(loop.timer) + def child(loop, signum): + procman.sigchild_handler(signum) # Create EventLoop eventloop.EventLoop.__init__(self, sock, signals={ signal.SIGINT: quit, signal.SIGTERM: quit, signal.SIGUSR1: reload_config, signal.SIGALRM: timer, + signal.SIGCHLD: child, }) # Create Dispatcher #TODO root.pymin = PyminHandler() @@ -72,6 +80,7 @@ class PyminDaemon(eventloop.EventLoop): def handle(self): r"handle() -> None :: Handle incoming events using the dispatcher." (msg, addr) = self.file.recvfrom(65535) + log.debug(u'PyminDaemon.handle: message %r from %r', msg, addr) try: result = self.dispatcher.dispatch(unicode(msg, 'utf-8')) if result is not None: @@ -83,12 +92,13 @@ class PyminDaemon(eventloop.EventLoop): except Exception, e: import traceback result = u'Internal server error\n' - traceback.print_exc() # TODO logging! response = u'ERROR ' + log.exception(u'PyminDaemon.handle: unhandled exception') if result is None: response += u'0\n' else: response += u'%d\n%s' % (len(result), result) + log.debug(u'PyminDaemon.handle: response %r to %r', response, addr) self.file.sendto(response.encode('utf-8'), addr) def handle_timer(self): @@ -97,6 +107,7 @@ class PyminDaemon(eventloop.EventLoop): def run(self): r"run() -> None :: Run the event loop (shortcut to loop())" + log.debug(u'PyminDaemon.loop()') # Start the timer self.handle_timer() signal.alarm(self.timer) @@ -104,10 +115,17 @@ class PyminDaemon(eventloop.EventLoop): try: return self.loop() except eventloop.LoopInterruptedError, e: + log.debug(u'PyminDaemon.loop: interrupted') pass if __name__ == '__main__': + logging.basicConfig( + level = logging.DEBUG, + format = '%(asctime)s %(levelname)-8s %(message)s', + datefmt = '%H:%M:%S', + ) + class Root(dispatcher.Handler): @handler(u"Print all the arguments, return nothing.") def test(self, *args):