ctlmgr: retry connection to master, print received ddb

This commit is contained in:
Sebastien Bourdeauducq 2015-02-06 18:58:54 +08:00
parent 17685d1e98
commit f9d3bd107e
1 changed files with 53 additions and 7 deletions

View File

@ -3,23 +3,57 @@
import asyncio import asyncio
import argparse import argparse
import os import os
import logging
from artiq.protocols.sync_struct import Subscriber from artiq.protocols.sync_struct import Subscriber
from artiq.tools import verbosity_args, init_logger
logger = logging.getLogger(__name__)
def get_argparser(): def get_argparser():
parser = argparse.ArgumentParser(description="ARTIQ controller manager") parser = argparse.ArgumentParser(description="ARTIQ controller manager")
verbosity_args(parser)
parser.add_argument( parser.add_argument(
"-s", "--server", default="::1", "-s", "--server", default="::1",
help="hostname or IP of the master to connect to") help="hostname or IP of the master to connect to")
parser.add_argument( parser.add_argument(
"--port", default=3250, type=int, "--port", default=3250, type=int,
help="TCP port to use to connect to the master") help="TCP port to use to connect to the master")
parser.add_argument(
"--retry-master", default=5.0, type=float,
help="retry timer for reconnecting to master")
return parser return parser
class Controllers:
def __setitem__(self, k, v):
print("set {} {}".format(k, v))
def __delitem__(self, k):
print("del {}".format(k))
def delete_all(self):
print("delete all")
class ControllerDB:
def __init__(self):
self.current_controllers = Controllers()
def sync_struct_init(self, init):
if self.current_controllers is not None:
self.current_controllers.delete_all()
for k, v in init.items():
self.current_controllers[k] = v
def main(): def main():
args = get_argparser().parse_args() args = get_argparser().parse_args()
init_logger(args)
controller_db = ControllerDB()
if os.name == "nt": if os.name == "nt":
loop = asyncio.ProactorEventLoop() loop = asyncio.ProactorEventLoop()
@ -27,14 +61,26 @@ def main():
else: else:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
try: try:
subscriber = Subscriber("master_ddb", lambda x: x) subscriber = Subscriber("devices", controller_db.sync_struct_init)
loop.run_until_complete(subscriber.connect(args.server, args.port)) while True:
try: try:
loop.run_forever() loop.run_until_complete(
finally: subscriber.connect(args.server, args.port))
loop.run_until_complete(subscriber.close()) try:
loop.run_until_complete(subscriber.receive_task)
finally:
loop.run_until_complete(subscriber.close())
except (ConnectionAbortedError, ConnectionError,
ConnectionRefusedError, ConnectionResetError) as e:
logger.warning("Connection to master failed (%s: %s)",
e.__class__.__name__, str(e))
else:
logger.warning("Connection to master lost")
logger.warning("Retrying in %.1f seconds", args.retry_master)
loop.run_until_complete(asyncio.sleep(args.retry_master))
finally: finally:
loop.close() loop.close()
controller_db.current_controllers.delete_all()
if __name__ == "__main__": if __name__ == "__main__":
main() main()