From 3b60162c1d4220c4ed8dbb74a8a3f9e223e0a103 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Tue, 7 Nov 2023 16:33:38 +0545 Subject: [PATCH] apscheduler used in place of fastapi background tasks for generating files --- src/backend/app/projects/project_routes.py | 52 +++++++++++++++++----- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index fd951d440e..78d51c44e2 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -631,20 +631,50 @@ async def generate_files( db, task_id=background_task_id, project_id=project_id ) - log.debug(f"Submitting {background_task_id} to background tasks stack") - background_tasks.add_task( + from datetime import datetime + + from apscheduler.schedulers.asyncio import AsyncIOScheduler + + sched = AsyncIOScheduler() + sched.start() + + job = sched.add_job( project_crud.generate_appuser_files, - db, - project_id, - extract_polygon, - contents, - extracts_contents if data_extracts else None, - xform_title, - file_ext[1:] if upload else "xls", - background_task_id, + "date", + run_date=datetime.now(), + args=[ + db, + project_id, + extract_polygon, + contents, + extracts_contents if data_extracts else None, + xform_title, + file_ext[1:] if upload else "xls", + background_task_id, + ], + id="unique_id", ) - return {"Message": f"{project_id}", "task_id": f"{background_task_id}"} + print("Job = ", job) + + # log.debug(f"Submitting {background_task_id} to background tasks stack") + # background_tasks.add_task( + # project_crud.generate_appuser_files, + # db, + # project_id, + # extract_polygon, + # contents, + # extracts_contents if data_extracts else None, + # xform_title, + # file_ext[1:] if upload else "xls", + # background_task_id, + # ) + + return { + "Message": f"{project_id}", + "task_id": f"{background_task_id}", + # "job":job + } @router.post("/update-form/{project_id}")