diff --git a/conjurer_librarian/requirements_librarian.txt b/conjurer_librarian/requirements_librarian.txt index 039f2d3..1d05f06 100644 --- a/conjurer_librarian/requirements_librarian.txt +++ b/conjurer_librarian/requirements_librarian.txt @@ -1,3 +1,3 @@ habanero waitress -flask \ No newline at end of file +flask[async] \ No newline at end of file diff --git a/conjurer_librarian/search_bot.py b/conjurer_librarian/search_bot.py index ec17436..e285979 100644 --- a/conjurer_librarian/search_bot.py +++ b/conjurer_librarian/search_bot.py @@ -68,7 +68,7 @@ def producer(out_q, control_q, filename, _logger): # IMPORTANT!!! ONLY ONE CONSUMER THREAD AS WE ARE NOT PUTTING SENTINELS BACK -def consumer(in_q, control_q, doi, live_results, result_list, control_dict, _logger): +def consumer(in_q, control_q, doi, live_results, result_list, control_dict, no, _logger): """ Consumes items from an input queue and checks if DOI exists in the live results list. @@ -79,6 +79,7 @@ def consumer(in_q, control_q, doi, live_results, result_list, control_dict, _log live_results (list): List to store the search results. _logger: Logger object for logging. """ + _logger.info("Consumer thread started:%s", no) for item in doi: result_list.append({"DOI": item[0], "exists": False, "data": item[1]}) while True: @@ -120,9 +121,9 @@ def search_for_doi(doi, live_results, _logger): threads = [] work_q = Queue(maxsize=WORK_Q_SIZE) control_q = Queue() - for _ in range (1, len(doi)//1000+1): + for i in range (0, (len(doi)//1000)+2): t_cons = Thread( - target=consumer, args=(work_q, control_q, doi, live_results, result_list, control_dict, _logger) + target=consumer, args=(work_q, control_q, doi, live_results, result_list, control_dict, i, _logger) ) _logger.info("Consumer thread created") threads.append(t_cons) diff --git a/conjurer_librarian/test_send.py b/conjurer_librarian/test_send.py new file mode 100644 index 0000000..e029f9c --- /dev/null +++ b/conjurer_librarian/test_send.py @@ -0,0 +1,45 @@ +import asyncio +import json +import logging +from json.decoder import JSONDecodeError +from logging import handlers +import requests + +MAIN_BOT_ADDRESS = "http://192.168.1.191:5000" +SEND_RESULTS = "/conjurer" + +async def send_results(): + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + h1 = handlers.RotatingFileHandler( + filename="D:\\logs\\librarian.log", + encoding="utf-8", + mode="a", + maxBytes=6 * 1024 * 1024, + backupCount=6, + ) + + logger.addHandler(h1) + + with open("s_results.json", "r+", encoding="utf-8") as s_file: + try: + database = json.load(s_file) + except JSONDecodeError: + pass + result = database["affd0dbc-e2c8-4173-b678-4103c0866495"] + result = {"affd0dbc-e2c8-4173-b678-4103c0866495":result} + + coroutine = asyncio.to_thread( + requests.post, + f"{MAIN_BOT_ADDRESS}{SEND_RESULTS}", + json=result, + timeout=360, + ) + logger.info("SENT") + result = await coroutine + logger.info(result.status_code) + logger.info("SEND CONFIRMED") +async def main(): + await send_results() +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file