From a7076c6ad4260aabd622038cdffbdb939f315959 Mon Sep 17 00:00:00 2001 From: A Noor <100anonyo@gmail.com> Date: Tue, 30 Apr 2024 02:38:58 +0000 Subject: [PATCH] fixed background querying --- routers/general.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/routers/general.py b/routers/general.py index e8e9a67..0036b3a 100644 --- a/routers/general.py +++ b/routers/general.py @@ -50,6 +50,10 @@ async def background_query(query_type, cik_list, background, query_function): cm.set_key_no_expiration(query_type, "running") for cik in cik_list: + if len(running) < 10: + background.add_task(query_function, cik, background) + running.append(cik) + while len(running) >= max_processes: for run in running: process = database.find_log(run, {"status": 1}) @@ -63,10 +67,6 @@ async def background_query(query_type, cik_list, background, query_function): await asyncio.sleep(5) - if len(running) < 10: - background.add_task(query_function, cik, background) - running.append(cik) - cm.set_key_no_expiration(query_type, "stopped") @@ -83,23 +83,11 @@ async def query_top(password: str, background: BackgroundTasks): return {"description": "Started querying filers."} -def create_error(cik, e): - stamp = str(datetime.now()) - cwd = os.getcwd() - with open(f"{cwd}/static/errors/error-general-{stamp}.log", "w") as f: - error_string = f"Failed to Query Filer {cik}\n{repr(e)}\n{format_exc()}" - f.write(error_string) - - @router.get("/restore", status_code=200) async def progressive_restore(password: str, background: BackgroundTasks): if password != os.environ["ADMIN_PASSWORD"]: raise HTTPException(detail="Unable to give access.", status_code=403) - restore = cm.get_key("restore") - if restore and restore == "running": - raise HTTPException(status_code=429, detail="Restore already running.") - filers = database.find_filers({}, {"cik": 1}) all_ciks = [filer["cik"] for filer in filers] @@ -108,6 +96,14 @@ async def progressive_restore(password: str, background: BackgroundTasks): return {"description": "Started progressive restore of filers."} +def create_error(cik, e): + stamp = str(datetime.now()) + cwd = os.getcwd() + with open(f"{cwd}/static/errors/error-general-{stamp}.log", "w") as f: + error_string = f"Failed to Query Filer {cik}\n{repr(e)}\n{format_exc()}" + f.write(error_string) + + @router.get("/backup", status_code=201) async def backup(password: str, background: BackgroundTasks): if password != os.environ["ADMIN_PASSWORD"]: