run_task.py 954 B

12345678910111213141516171819202122232425
  1. import datetime
  2. import logging
  3. from app.core.runner.pub_handler import StreamEventHandler
  4. from app.core.runner.thread_runner import ThreadRunner
  5. from app.providers.celery_app import celery_app
  6. from app.providers.database import session
  7. from app.services.run.run import RunService
  8. @celery_app.task(bind=True, autoretry_for=())
  9. def run_task(self, run_id: str, token_id: str, stream: bool = False):
  10. logging.info(
  11. f"[run_task] [{run_id}] [token_id] [{token_id}] running at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
  12. )
  13. try:
  14. ThreadRunner(run_id, token_id, session, stream).run()
  15. except Exception as e:
  16. print("aawwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww")
  17. logging.exception(e)
  18. StreamEventHandler(run_id=run_id, is_stream=True).pub_error(str(e))
  19. RunService.to_failed(session=session, run_id=run_id, last_error=e)
  20. finally:
  21. session.close()
  22. return None