From cd0725acf326df7d3ce20297a48b3ce7a007dd24 Mon Sep 17 00:00:00 2001 From: Ivan Vazhenin Date: Sun, 2 Apr 2023 12:39:42 +0300 Subject: [PATCH] Fix Request/Answer queries --- main.py | 41 +++++++++++++++++++++++++++++++---------- reqs/graphql.py | 25 +++++++++++++------------ 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 8286d94..3fd073d 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,6 @@ +import asyncio +import threading +from queue import Queue import datetime import json from uuid import uuid4, UUID @@ -15,6 +18,14 @@ import zipfile PASSWORD = 'gost_2012$a742ec53198ec2a5027086fba8814a89982a57112d1a72d02260161108f39b50' +tasks = Queue() + + +def run_tasks(): + while True: + task = tasks.get() + task() + # Expose a function def list_contents(dir_name): @@ -45,6 +56,7 @@ def restore_uuid(oid): def load_catalog(params, files, url): + print('load_catalog') date = datetime.datetime.now() rxmls = RequestXmlService() req = ET.fromstring(params['query_data']) @@ -62,6 +74,7 @@ def load_catalog(params, files, url): 'query_data': ET.tostring(res, encoding='unicode', xml_declaration=True) } catalog = get_catalog() + print('Catalog_loaded') filename = uuid4().hex filepath = '/tmp/' + filename zipf = zipfile.ZipFile(filepath, "w") @@ -161,15 +174,19 @@ def get_metadata(params, files, url): proxy.send(response_params, response_files, 'http://10.10.8.27:9000/') +def run_task(query_type, params, files, url): + if query_type == 4: + tasks.put(lambda: load_catalog(params, files, url)) + if query_type == 1: + tasks.put(lambda: get_objects(params, files, url)) + if query_type == 24: + tasks.put(lambda: get_metadata(params, files, url)) + + def accept(params, files, url): print(params, files, url) print('Accept') - if params['query_type'] == 4: - load_catalog(params, files, url) - if params['query_type'] == 1: - get_objects(params, files, url) - if params['query_type'] == 24: - get_metadata(params, files, url) + run_task(params['query_type'], params, files, url) return True @@ -188,6 +205,9 @@ def main(): server.register_function(accept) server.register_function(onSent) + thread = threading.Thread(target=run_tasks) + thread.start() + try: server.serve_forever() except KeyboardInterrupt: @@ -195,11 +215,12 @@ def main(): def test(): - params = {"from": "tcp://kptsp_vb", "query_data": "
", "query_type": 24, "to": "tcp://bnd127", "user_id": "3302", "ts_added": 1679825320.653038} - files = [] - url = 'http://127.0.0.1:7000/xmlrpc' + #params = {"from": "tcp://kptsp_vb", "query_data": "
", "query_type": 24, "to": "tcp://bnd127", "user_id": "3302", "ts_added": 1679825320.653038} + #files = [] + #url = 'http://127.0.0.1:7000/xmlrpc' # accept(params, files, url) - get_metadata(params, files, url) + #get_metadata(params, files, url) + get_catalog() if __name__ == '__main__': diff --git a/reqs/graphql.py b/reqs/graphql.py index 9e4ae94..4005463 100644 --- a/reqs/graphql.py +++ b/reqs/graphql.py @@ -2,43 +2,44 @@ from gql import gql, Client from gql.transport.aiohttp import AIOHTTPTransport transport = AIOHTTPTransport(url="https://gql.ivazh.ru/graphql/") +service = 'pdim' def get_classifier(): - client = Client(transport=transport, fetch_schema_from_transport=True) + client = Client(transport=transport, fetch_schema_from_transport=True, execute_timeout=None) query = gql( """ - query getClassifier { - getClassifier(name: "ood") + query getClassifier($name: String!) { + getClassifier(name: $name) } """ ) - result = client.execute(query) + result = client.execute(query, variable_values={"name": service}, ) return result['getClassifier'] def get_catalog(): - client = Client(transport=transport, fetch_schema_from_transport=True) + client = Client(transport=transport, fetch_schema_from_transport=True, execute_timeout=None) query = gql( """ - query getCatalog { - getCatalog(name: "ood") + query getCatalog($name: String!) { + getCatalog(name: $name) } """ ) - result = client.execute(query) + result = client.execute(query, variable_values={"name": service}) return result['getCatalog'] def get_object(oid: str): - client = Client(transport=transport, fetch_schema_from_transport=True) + client = Client(transport=transport, fetch_schema_from_transport=True, execute_timeout=None) query = gql( """ - query getObjects($oid: String!) { - getObject(name: "ood", oid: $oid) + query getObjects($oid: String!, $name: String!) { + getObject(name: $name, oid: $oid) } """ ) - params = {'oid': oid} + params = {'oid': oid, 'name': service} result = client.execute(query, variable_values=params) return result['getObject']