run_task.py 890 B

1234567891011121314151617181920212223
  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, stream: bool = False):
  10. logging.info(f"[run_task] [{run_id}] running at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
  11. try:
  12. ThreadRunner(run_id, session, stream).run()
  13. except Exception as e:
  14. print("aawwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww")
  15. logging.exception(e)
  16. StreamEventHandler(run_id=run_id, is_stream=True).pub_error(str(e))
  17. RunService.to_failed(session=session, run_id=run_id, last_error=e)
  18. finally:
  19. session.close()
  20. return None