def main(args): log = logging.getLogger("addisonarches.web") log.setLevel(args.log_level) formatter = logging.Formatter( "%(asctime)s %(levelname)-7s %(name)s|%(message)s") ch = logging.StreamHandler() if args.log_path is None: ch.setLevel(args.log_level) else: fh = WatchedFileHandler(args.log_path) fh.setLevel(args.log_level) fh.setFormatter(formatter) log.addHandler(fh) ch.setLevel(logging.WARNING) ch.setFormatter(formatter) log.addHandler(ch) loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) #TODO: Read service name from CLI service = "dev" # Cf qa, demo, prod, etc tok = token(args.connect, service, APP_NAME) node = create_udp_node(loop, tok, down, up) loop.create_task(node(token=tok)) app = aiohttp.web.Application() assets = Assets(app, **vars(args)) reg = Registration(app, tok, down, up, **vars(args)) transitions = Transitions(app, **vars(args)) work = Workflow(app, tok, down, up, **vars(args)) for svc in (assets, reg, transitions, work): log.info("{0.__class__.__name__} object serves {1}".format( svc, ", ".join(svc.routes.keys()))) handler = app.make_handler() f = loop.create_server(handler, args.host, args.port) srv = loop.run_until_complete(f) log.info("Serving on {0[0]}:{0[1]}".format(srv.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: pass finally: loop.run_until_complete(handler.finish_connections(1.0)) srv.close() loop.run_until_complete(srv.wait_closed()) loop.run_until_complete(app.finish()) loop.close()
def main(args): log = logging.getLogger(APP_NAME) log.setLevel(args.log_level) formatter = logging.Formatter( "%(asctime)s %(levelname)-7s %(name)s|%(message)s") ch = logging.StreamHandler() if args.log_path is None: ch.setLevel(args.log_level) else: fh = WatchedFileHandler(args.log_path) fh.setLevel(args.log_level) fh.setFormatter(formatter) log.addHandler(fh) ch.setLevel(logging.WARNING) ch.setFormatter(formatter) log.addHandler(ch) loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) tok = token(args.connect, APP_NAME) node = create_udp_node(loop, tok, down, up) loop.create_task(node(token=tok)) loop.create_task(queue_logger(loop, up)) msg = parcel( tok, Alert(datetime.datetime.now(), "Hello World!"), via=Address(tok.namespace, tok.user, tok.service, turberfield.ipc.demo.router.APP_NAME) ) log.info("Sending message: {}".format(msg)) loop.call_soon_threadsafe(functools.partial(down.put_nowait, msg)) try: loop.run_forever() except KeyboardInterrupt: for task in asyncio.Task.all_tasks(loop=loop): task.cancel() for resource in resources: resource.close() finally: loop.close()
def main(args): loop = asyncio.SelectorEventLoop() log = logging.getLogger(log_setup(args, loop=loop)) asyncio.set_event_loop(loop) down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) #TODO: Read service name from CLI service = "dev" # Cf qa, demo, prod, etc tok = token(args.connect, service, args.session) node = create_udp_node(loop, tok, down, up) loop.create_task(node(token=tok)) progress, down, up = addisonarches.game.create( args.output, args.session, args.name, tok, down=down, up=up, loop=loop ) loop.run_forever()
def main(args): log = logging.getLogger(APP_NAME) log.setLevel(args.log_level) formatter = logging.Formatter( "%(asctime)s %(levelname)-7s %(name)s|%(message)s") ch = logging.StreamHandler() if args.log_path is None: ch.setLevel(args.log_level) else: fh = WatchedFileHandler(args.log_path) fh.setLevel(args.log_level) fh.setFormatter(formatter) log.addHandler(fh) ch.setLevel(logging.WARNING) ch.setFormatter(formatter) log.addHandler(ch) loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) tok = token(args.connect, APP_NAME) node = create_udp_node(loop, tok, down, up) loop.create_task(node(token=tok)) log.info("Starting router node...") try: loop.run_forever() except KeyboardInterrupt: for task in asyncio.Task.all_tasks(loop=loop): task.cancel() for resource in resources: resource.close() finally: loop.close()
def game_setup(loop, output): down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) tok = token( GameTests.connect, "test", "addisonarches.test.game" ) node = create_udp_node(loop, tok, down, up) loop.create_task(node(token=tok)) game, clock, down, up = create_game( output, user=GameTests.user, name="test", down=down, up=up, loop=loop ) progress, down, up = init_game( game, clock, down, up, loop=self.loop ) # Tests start at Addison Arches game.location = game.home return progress
def run_test_async(self, coro, loop=None): self.assertTrue(loop) def run_then_cancel(coro, progress, down, up, loop): yield from asyncio.sleep(0, loop=loop) try: yield from coro(progress, down, up, loop=loop) finally: msg = parcel( self.token, Alert(datetime.datetime.now(), "Hello World!"), dst=Address( self.token.namespace, self.token.user, self.token.service, "addisonarches.test.game" ) ) yield from down.put(msg) yield from asyncio.sleep(0, loop=loop) for task in asyncio.Task.all_tasks(loop=loop): task.cancel() def game_setup(loop, output): down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) tok = token( GameTests.connect, "test", "addisonarches.test.game" ) node = create_udp_node(loop, tok, down, up) loop.create_task(node(token=tok)) game, clock, down, up = create_game( output, user=GameTests.user, name="test", down=down, up=up, loop=loop ) progress, down, up = init_game( game, clock, down, up, loop=self.loop ) # Tests start at Addison Arches game.location = game.home return progress progress = game_setup(loop, self.root.name) down = asyncio.Queue(loop=loop) up = asyncio.Queue(loop=loop) node = create_udp_node(loop, self.token, down, up) loop.create_task(node(token=self.token)) test = loop.create_task( run_then_cancel(coro, progress, down, up, loop=loop) ) try: rv = loop.run_until_complete(asyncio.wait_for(test, 3, loop=loop)) except concurrent.futures.CancelledError: rv = None finally: loop.close() e = test.exception() if e is not None: if isinstance(e, UserWarning): print(e, file=sys.stderr) else: raise e else: return rv