-
Notifications
You must be signed in to change notification settings - Fork 0
Description
ISSUE_NUMBER: GH-2
Description
The run_cron_jobs function in app.py has potential inefficiencies and lacks a timeout for the run_job_expiration function.
File: repositories/jobflowapi/app.py
Line:
99-121
Severity: medium
Current Behavior
The run_cron_jobs function creates a new event loop every time the cron job runs and doesn't specify a timeout for the run_job_expiration function.
Expected Behavior
The run_cron_jobs function should reuse the same event loop for subsequent runs and specify a timeout for the run_job_expiration function.
Suggested Fix
Create the event loop once outside the while loop and reuse it for subsequent runs. Add a timeout to the run_job_expiration function to prevent it from hanging indefinitely.
Code Context
def run_cron_jobs():
logger.info("Starting cron job thread")
while True:
now = datetime.now()
# Run job expiration every day at 1:00 AM (1 0 * * *)
if pycron.is_now('0 1 * * *'):
logger.info("Running scheduled job expiration")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
expired_count = loop.run_until_complete(run_job_expiration())
logger.info(f"Job expiration complete. Total jobs expired: {expired_count}")
except Exception as e:
logger.error(f"Error running job expiration: {str(e)}")
finally:
loop.close()
time.sleep(60)Additional Notes
Reusing the event loop improves efficiency, and adding a timeout prevents the cron job thread from being blocked indefinitely.