async def run(self): """ Main loop to spawn http and https servers and handle signal interrupts """ print("Server starting up") async with SignalQueue(signal.SIGHUP, signal.SIGINT, signal.SIGTERM) as sig: while True: # Spin up tcp servers if settings.ENABLE_HTTP: serve_http_task = await spawn(tcp_server, "localhost", settings.HTTP_PORT, self.serve_http) if settings.ENABLE_HTTPS: serve_https_task = await spawn(tcp_server, "localhost", settings.HTTPS_PORT, self.serve_https) # wait for signal intterupts signo = await sig.get() await serve_http_task.cancel() await serve_https_task.cancel() if signo == signal.SIGHUP: print("Server restarting") # TODO reload configuration else: print("Server shutting down") break
async def watch_signal(self): while True: # term for warm shutdown # int for cold shutdown # hup for reload ss = [signal.SIGTERM, signal.SIGINT, signal.SIGCHLD, signal.SIGHUP] sname = {i.value: i.name for i in ss} with SignalQueue(*ss) as sq: signo = await sq.get() self.logger.info('get signal: %s' % sname[signo]) if signo != signal.SIGCHLD: if signo == signal.SIGTERM: self.logger.info('kill myself...warm shutdown') await self.shutdown() elif signo == signal.SIGINT: self.logger.info('kill myself...cold shutdown') await self.shutdown(warm=False) elif signo == signal.SIGHUP: self.logger.info('reloading...') # TODO: reload, restart? pass break else: self.worker_pool.reap_workers() continue return
async def main(host, port): async with SignalQueue(signal.SIGHUP) as restart: while True: log.info('Starting the server') serv_task = await spawn(chat_server, host, port) await restart.get() log.info('Server shutting down') await serv_task.cancel()
async def main(host, port): async with SignalQueue(signal.SIGHUP) as restart: while True: print('Starting the server') server = await spawn(tcp_server, host, port, client_handler) await restart.get() print('Server shutting down') await server.cancel()
async def main(host, port): async with SignalQueue(signals.SIGHUP, signals.SIGTERM) as close_signals: logger.info('Starting the server') serv_task = await spawn(server, host, port) # Stop here and wait for any of the close signals signal = await close_signals.get() logger.info('Server shutting down: %s', signal) # cancle all server tasks await serv_task.cancel()