We are using fastapi-utils to have a scheduled task in the background. We check all 5 seconds if new data is available in the DB, if yes we process it (takes up to 5 minutes)
During this time, the couroutine should be blocking so that its only triggered once.
We noticed that our data is sometimes processed 3x, we assume that the scheduler continues to run, even though the function has been triggered.
Therefore we tried to circumvent it with the IsRunningQuery variable.
We tried a solution with a while True loop without @repeat_every to make it run once at startup, but Azure Webapps does not allow running this.
@app.on_event("startup")
@repeat_every(wait_first=True,seconds=int(10))
def scheduled_task() -> None:
global IsRunningQuery
global LastCheck
if IsRunningQuery == False:
IsRunningQuery = True
gunicorn_logger.info("status='checkforleads'")
OurProccessingClass.processDataBaseData() # can take up 5 minutes
LastCheck=Utils.datetime()
IsRunningQuery = False
This variante works in our DEV environment, but not on Azure
@app.on_event("startup")
async def scheduled_task() -> None:
while True:
gunicorn_logger.info("status='checkforleads'")
OurProccessingClass.processDataBaseData() # can take up 5 minutes
time.sleep(int(os.environ["CRM_SLEEP"]))