def __init__(self): self.cfg = Config() self.cfg.with_env() self.booker_server = None self.db_engine = None self.gateways_clients = {} self.order_processing: OrdersProcessor
async def _get_db_engine(): cfg = Config() cfg.with_env() db_engine = await create_db_engine( host=cfg.db_host, port=cfg.db_port, user=cfg.db_user, password=cfg.db_password, database=cfg.db_database, ) return db_engine
def test_config_with_env(): if os.path.isfile(str(project_root_dir) + "/.env"): c = Config() c.with_env() else: copyfile( str(project_root_dir) + "/.env.example", str(project_root_dir) + "/.env") c = Config() c.with_env() os.remove(str(project_root_dir) + "/.env") assert c.db_port != Config.http_port assert c.http_port != Config.http_port
def test_config(): c = Config() assert c.http_port == 8080
class AppContext: def __init__(self): self.cfg = Config() self.cfg.with_env() self.booker_server = None self.db_engine = None self.gateways_clients = {} self.order_processing: OrdersProcessor def run(self): loop = asyncio.get_event_loop() signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT) for s in signals: loop.add_signal_handler(s, lambda sig=s: asyncio.create_task( self.shutdown(loop, signal=s))) loop.set_exception_handler(self.ex_handler) try: self.db_engine: DBEngine = loop.run_until_complete( create_db_engine( host=self.cfg.db_host, port=self.cfg.db_port, user=self.cfg.db_user, password=self.cfg.db_password, database=self.cfg.db_database, )) log.info( f"Database connected to {self.cfg.db_host}:{self.cfg.db_port}") except Exception as ex: log.warning(f"Unable to connect database: {ex}") self.booker_server = BookerServer(ctx=self, host=self.cfg.http_host, port=self.cfg.http_port) loop.run_until_complete(self.booker_server.start()) log.info( f"Booker server started on {self.cfg.http_host}:{self.cfg.http_port}" ) for name, data in self.cfg.gateways.items(): self.gateways_clients[name] = {} log.info(f"Setup {name} gateways clients connections...") for side, params in data.items(): if params: gw_client = BookerSideClient(name, side, self, params[0], params[1]) self.gateways_clients[name][side] = gw_client try: loop.run_until_complete( gw_client.connect(params[0], params[1], "/ws-rpc")) log.info( f"{side}{name} client created and ready to connect ws://{params[0]}:{params[1]}/ws-rpc" ) loop.run_until_complete(gw_client.disconnect()) except Exception as ex: log.warning( f"{side}{name} created, but unable to connect ws://{params[0]}:{params[1]}/ws-rpc: {ex}" ) else: log.info(f"{side}{name} client not specified") self.order_processing = OrdersProcessor(self) loop.create_task(self.order_processing.run()) try: loop.run_forever() finally: log.info("Successfully shutdown the app") loop.close() def ex_handler(self, loop, ex_context): ex = ex_context.get("exception") coro_name = ex_context["future"].get_coro().__name__ log.exception(f"{ex.__class__.__name__} in {coro_name}: {ex_context}") coro_to_restart = None # TODO write to-restart coro parsing logic if coro_to_restart: log.info(f"Trying to restart {coro_to_restart.__name__} coroutine") loop.create_task(coro_to_restart()) @staticmethod async def shutdown(loop, signal=None): if signal: log.info(f"Received exit signal {signal.name}...") else: log.info("No exit signal") tasks = [ t for t in asyncio.all_tasks() if t is not asyncio.current_task() ] log.info(f"Cancelling {len(tasks)} outstanding tasks") for task in tasks: task.cancel() await asyncio.gather(*tasks, return_exceptions=True) log.info(f"Make some post-shutdown things") loop.stop()