ctlmgr: clean shutdown

This commit is contained in:
Sebastien Bourdeauducq 2015-02-08 21:44:49 +08:00
parent a50c74523f
commit 542e047f5a
1 changed files with 19 additions and 3 deletions

View File

@ -113,6 +113,12 @@ class Controllers:
for name in set(self.active_or_queued): for name in set(self.active_or_queued):
del self[name] del self[name]
@asyncio.coroutine
def shutdown(self):
self.process_task.cancel()
for c in self.active.values():
yield from c.end()
class ControllerDB: class ControllerDB:
def __init__(self, retry_command): def __init__(self, retry_command):
@ -153,21 +159,31 @@ def ctlmgr(server, port, retry_master, retry_command):
logger.warning("Connection to master lost") logger.warning("Connection to master lost")
logger.warning("Retrying in %.1f seconds", retry_master) logger.warning("Retrying in %.1f seconds", retry_master)
yield from asyncio.sleep(retry_master) yield from asyncio.sleep(retry_master)
except asyncio.CancelledError:
pass
finally: finally:
controller_db.current_controllers.delete_all() yield from controller_db.current_controllers.shutdown()
def main(): def main():
args = get_argparser().parse_args() args = get_argparser().parse_args()
init_logger(args) init_logger(args)
if os.name == "nt": if os.name == "nt":
loop = asyncio.ProactorEventLoop() loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
else: else:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
try: try:
loop.run_until_complete(ctlmgr(args.server, args.port, task = asyncio.Task(ctlmgr(
args.retry_master, args.retry_command)) args.server, args.port, args.retry_master, args.retry_command))
try:
loop.run_forever()
finally:
task.cancel()
loop.run_until_complete(asyncio.wait_for(task, None))
finally: finally:
loop.close() loop.close()