X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/6e49be26bf70a6fcd4b29ffc8cb270edf2e3c500..55efba31a241826ed31597b14f2b5eb9efe808f1:/pymin/pymindaemon.py diff --git a/pymin/pymindaemon.py b/pymin/pymindaemon.py index a65f19c..067a727 100644 --- a/pymin/pymindaemon.py +++ b/pymin/pymindaemon.py @@ -16,6 +16,7 @@ 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 @@ -44,27 +45,37 @@ 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 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(bind_addr) - # Create EventLoop - eventloop.EventLoop.__init__(self, sock, timer=timer) - # Create Dispatcher - #TODO root.pymin = PyminHandler() - self.dispatcher = dispatcher.Dispatcher(root) # Signal handling - def quit(signum, frame): + def quit(loop, signum): log.debug(u'PyminDaemon quit() handler: signal %r', signum) log.info(u'Shutting down...') - self.stop() # tell main event loop to stop - def reload_config(signum, frame): + loop.stop() # tell main event loop to stop + def reload_config(loop, signum): log.debug(u'PyminDaemon reload_config() handler: signal %r', signum) log.info(u'Reloading configuration...') # TODO iterate handlers list propagating reload action - signal.signal(signal.SIGINT, quit) - signal.signal(signal.SIGTERM, quit) - signal.signal(signal.SIGUSR1, reload_config) + 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() + self.dispatcher = dispatcher.Dispatcher(root) def handle(self): r"handle() -> None :: Handle incoming events using the dispatcher." @@ -97,6 +108,10 @@ 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) + # Loop try: return self.loop() except eventloop.LoopInterruptedError, e: