run_task.py 1.1 KB

1234567891011121314151617181920212223242526
  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=(Exception,), max_retries=3, default_retry_delay=60)
  9. #@celery_app.task(bind=True, max_retries=3, default_retry_delay=60)
  10. @celery_app.task(bind=True, autoretry_for=())
  11. def run_task(self, run_id: str, token_id: str, stream: bool = False):
  12. logging.info(
  13. f"[run_task] [{run_id}] [token_id] [{token_id}] running at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
  14. )
  15. try:
  16. ThreadRunner(run_id, token_id, session, stream).run()
  17. except Exception as e:
  18. print("aawwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww")
  19. logging.exception(e)
  20. StreamEventHandler(run_id=run_id, is_stream=True).pub_error(str(e))
  21. RunService.to_failed(session=session, run_id=run_id, last_error=e)
  22. finally:
  23. session.close()
  24. return None