task_scheduler.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from applications.utils import task_schedule_response
  2. from applications.tasks.monitor_tasks import check_kimi_balance
  3. class TaskScheduler:
  4. def __init__(self, data, log_service):
  5. self.data = data
  6. self.log_client = log_service
  7. async def deal(self):
  8. task_name = self.data.get("task_name")
  9. if not task_name:
  10. await self.log_client.log(
  11. contents={
  12. "task": task_name,
  13. "function": "task_scheduler_deal",
  14. "message": "not task name in params",
  15. "status": "fail",
  16. "data": self.data,
  17. }
  18. )
  19. return await task_schedule_response.fail_response(
  20. error_code="4002", error_message="task_name must be input"
  21. )
  22. match task_name:
  23. case "check_kimi_balance":
  24. response = await check_kimi_balance()
  25. await self.log_client.log(
  26. contents={
  27. "task": task_name,
  28. "function": "task_scheduler_deal",
  29. "message": "check_kimi_balance task execute successfully",
  30. "status": "success",
  31. "data": response,
  32. }
  33. )
  34. return await task_schedule_response.success_response(
  35. task_name=task_name, data=response
  36. )
  37. case _:
  38. await self.log_client.log(
  39. contents={
  40. "task": task_name,
  41. "function": "task_scheduler_deal",
  42. "message": "wrong task input",
  43. "status": "success",
  44. "data": self.data,
  45. }
  46. )
  47. return await task_schedule_response.fail_response(
  48. error_code="4001", error_message="wrong task name input"
  49. )