From ce40243de49f2dc1f85ac9bf5091485a4efd463e Mon Sep 17 00:00:00 2001 From: tobtoht Date: Tue, 29 Dec 2020 21:33:32 +0100 Subject: [PATCH] Load nodes from cache --- fapi/factory.py | 8 ++++---- fapi/tasks/__init__.py | 10 ++++++++++ fapi/tasks/rpc_nodes.py | 4 +++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/fapi/factory.py b/fapi/factory.py index 096b89f..115d820 100644 --- a/fapi/factory.py +++ b/fapi/factory.py @@ -17,16 +17,16 @@ import settings now = datetime.now() app: Quart = None cache = None -rpc_nodes: dict = None user_agents: List[str] = None connected_websockets: Set[asyncio.Queue] = set() _is_primary_worker_thread = False async def _setup_nodes(app: Quart): - global rpc_nodes - with open("data/nodes.json", "r") as f: - rpc_nodes = json.loads(f.read()).get(settings.COIN_SYMBOL) + global cache + with open('data/nodes.json', 'r') as f: + nodes = json.loads(f.read()).get(settings.COIN_SYMBOL) + cache.execute('JSON.SET', 'nodes', '.', json.dumps(nodes)) async def _setup_user_agents(app: Quart): diff --git a/fapi/tasks/__init__.py b/fapi/tasks/__init__.py index 18dab97..e9de64d 100644 --- a/fapi/tasks/__init__.py +++ b/fapi/tasks/__init__.py @@ -123,6 +123,16 @@ class FeatherTask: async def end(self, result: dict): raise NotImplementedError() + async def cache_json_get(self, key: str, path="."): + from fapi.factory import app, cache + + try: + data = await cache.execute('JSON.GET', key, path) + if data: + return json.loads(data) + except Exception as ex: + app.logger.error(f"Redis error: {ex}") + async def cache_get(self, key: str) -> dict: from fapi.factory import app, cache diff --git a/fapi/tasks/rpc_nodes.py b/fapi/tasks/rpc_nodes.py index 8eb45e6..225c842 100644 --- a/fapi/tasks/rpc_nodes.py +++ b/fapi/tasks/rpc_nodes.py @@ -24,13 +24,15 @@ class RPCNodeCheckTask(FeatherTask): async def task(self) -> List[dict]: """Check RPC nodes status""" - from fapi.factory import app, rpc_nodes, cache + from fapi.factory import app, cache try: heights = json.loads(await cache.get("blockheights")) except: heights = {} + rpc_nodes = await self.cache_json_get("nodes") + nodes = [] for network_type_coin, _ in rpc_nodes.items(): data = []