From ea1198a8984ca169dc00e3d6438e275977168177 Mon Sep 17 00:00:00 2001 From: William Luke Date: Thu, 23 Dec 2021 10:36:29 +0300 Subject: [PATCH] add catch to celery wrapper --- .../cic_eth/runnable/daemons/server.py | 11 +++++-- apps/cic-eth/cic_eth/server/celery.py | 30 ++++++++++------- apps/cic-eth/test_requirements.txt | 1 - apps/cic-eth/tests/test_server.py | 32 ++++++++++++------- docker-compose.yml | 1 + 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/apps/cic-eth/cic_eth/runnable/daemons/server.py b/apps/cic-eth/cic_eth/runnable/daemons/server.py index 389fe553..3aeabf36 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/server.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/server.py @@ -114,11 +114,16 @@ def token(token_symbol: str, proof: Optional[str] = None): return token -@app.get("/tokens", response_model=Token) +@app.get("/tokens", response_model=List[Token]) def tokens(token_symbols: Optional[List[str]] = Query(None), proof: Optional[Union[str, List[str], List[List[str]]]] = None): - data = celery.call('tokens', token_symbols, proof=proof) + data = celery.call('tokens', token_symbols, + catch=len(token_symbols), proof=proof) if data: - return Token.new(data) + tokens = [] + for token in data: + print(f"Token: {token}") + tokens.append(Token.new(token)) + return tokens return None diff --git a/apps/cic-eth/cic_eth/server/celery.py b/apps/cic-eth/cic_eth/server/celery.py index d6dd9cd4..74404c03 100644 --- a/apps/cic-eth/cic_eth/server/celery.py +++ b/apps/cic-eth/cic_eth/server/celery.py @@ -23,8 +23,8 @@ redis_port = config.get('REDIS_PORT') redis_db = config.get('REDIS_DB') -def call(method, *args, **kwargs): - """ Creates a redis channel and calls `cic_eth.api` with the provided `method` and `*args`. Returns the result of the api call +def call(method, *args, catch=1, **kwargs): + """ Creates a redis channel and calls `cic_eth.api` with the provided `method` and `*args`. Returns the result of the api call. Catch allows you to specify how many messages to catch before returning. """ log.debug(f"Using chainspec: {chain_spec}") redis_channel = str(uuid.uuid4()) @@ -43,21 +43,27 @@ def call(method, *args, **kwargs): ps.get_message() try: - o = ps.get_message(timeout=config.get('REDIS_TIMEOUT')) + data = [] + if catch == 1: + message = ps.get_message(timeout=config.get('REDIS_TIMEOUT')) + data = json.loads(message['data'])["result"] + else: + for _i in range(catch): + message = ps.get_message(timeout=config.get('REDIS_TIMEOUT')) + result = json.loads(message['data'])["result"] + data.append(result) + except TimeoutError as e: sys.stderr.write( f"cic_eth.api.{method}({args}, {kwargs}) timed out:\n {e}") + raise e + except Exception as e: + sys.stderr.write( + f'Unable to parse Data:\n{data}\n Error:\n{e}') return None log.debug( - f"cic_eth.api.{method}(args={args}, kwargs={kwargs})\n {o}") + f"cic_eth.api.{method}(args={args}, kwargs={kwargs})\n {data}") ps.unsubscribe() - try: - result = json.loads(o['data'])["result"] - return result - - except Exception as e: - sys.stderr.write( - f'Unable to parse Data:\n{o}\n Error:\n{e}') - return None + return data diff --git a/apps/cic-eth/test_requirements.txt b/apps/cic-eth/test_requirements.txt index de8c7c01..67462190 100644 --- a/apps/cic-eth/test_requirements.txt +++ b/apps/cic-eth/test_requirements.txt @@ -2,7 +2,6 @@ pytest==6.0.1 pytest-celery==0.0.0a1 pytest-mock==3.3.1 pytest-cov==2.10.1 -pytest-localserver==0.5.1 pytest-redis==2.0.0 redis==3.5.3 eth-tester==0.5.0b3 diff --git a/apps/cic-eth/tests/test_server.py b/apps/cic-eth/tests/test_server.py index bf5aa64e..f45bf71d 100644 --- a/apps/cic-eth/tests/test_server.py +++ b/apps/cic-eth/tests/test_server.py @@ -49,22 +49,30 @@ def test_tokens(): log.debug(f"tokens response {response}") tokens = response.json() - assert tokens == { - 'address': '3ff776b6f888980def9d4220858803f9dc5e341e', - 'converters': [], - 'decimals': 6, - 'name': 'Giftable Token', - 'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'], - 'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328', - 'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}], - 'symbol': 'GFT', - } + assert tokens == [ + {'address': '3ff776b6f888980def9d4220858803f9dc5e341e', + 'converters': [], + 'decimals': 6, + 'name': 'Giftable Token', + 'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'], + 'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328', + 'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}], + 'symbol': 'GFT'}, + {'address': '9c9506cf6c50f5b0371be72920fc6060d1a88a6a', + 'converters': [], + 'decimals': 6, + 'name': 'Coffee', + 'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'], + 'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3', + 'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', + 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}], + 'symbol': 'COFE'}, + ] def test_account(): # Default Token - response = client.get('/default_token', - ) + response = client.get('/default_token') log.debug(f"balance response {response}") default_token = response.json() diff --git a/docker-compose.yml b/docker-compose.yml index 3f8ec734..81930c2e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -205,6 +205,7 @@ services: if [[ -f /tmp/cic/config/env_reset ]]; then source /tmp/cic/config/env_reset; fi set +a python -m cic_eth.runnable.daemons.server + cic-eth-tracker: image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-eth:${TAG:-latest} build: