decodeTask.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from loguru import logger
  2. import sys
  3. import time
  4. from utils.sync_mysql_help import mysql
  5. from typing import Dict, Any
  6. from utils.params import DecodeWorkflowParam
  7. from src.workflows.decode_workflow import DecodeWorkflow
  8. logger.add(sink=sys.stderr, level="ERROR", backtrace=True, diagnose=True)
  9. def invoke_decode_workflow(task_params: Dict[str, Any]):
  10. """主函数"""
  11. result = DecodeWorkflow(task_params)
  12. if result:
  13. return result
  14. else:
  15. print(f"❌ 保存结果失败,但将继续处理")
  16. return None
  17. def get_decode_result_by_id(task_id:str):
  18. sql = "SELECT * FROM decode_videos WHERE task_id = %s AND task_status = 2 "
  19. tasks = mysql.fetchone(sql, (task_id,))
  20. if not tasks:
  21. logger.info(f"task_id = {task_id} , 任务不存在")
  22. return None
  23. return tasks['result'], tasks['status']
  24. def decode_task_status_handler():
  25. # 从数据库中获取任务,每次获取一个
  26. sql = "SELECT * FROM decode_record WHERE task_status = 0 "
  27. """json"""
  28. task = mysql.fetchone(sql)
  29. if not task:
  30. logger.info("任务列表为空")
  31. return
  32. else:
  33. task_id = task['task_id']
  34. sql = "UPDATE decode_record SET task_status = 1 WHERE task_id = %s"
  35. mysql.execute(sql, (task_id,))
  36. # 获取任务结果
  37. try:
  38. task_id = task['task_id']
  39. video_url = task['video_url']
  40. video_id = task['video_id']
  41. task_params = {'task_id':task_id, 'video_id':video_id, 'video_url':video_url}
  42. logger.info(f"task_id = {task_id} , video_id = {video_id}")
  43. task_create_timestamp = task['create_timestamp']
  44. current_timestamp = int(time.time() * 1000)
  45. decode_result = invoke_decode_workflow(task_params)
  46. if decode_result:
  47. # 更新任务状态为2,任务完成
  48. sql = "UPDATE decode_record SET task_status = 2, WHERE task_id = %s"
  49. mysql.execute(sql, (task_id))
  50. logger.info(f"task_id = {task_id} , decode_result = {decode_result}")
  51. else:
  52. if current_timestamp - task_create_timestamp > 1000 * 60 * 60:
  53. sql = "UPDATE decode_record SET task_status = 3, WHERE task_id = %s"
  54. mysql.execute(sql, (task_id))
  55. logger.info(f"task_id = {task_id} ,任务状态异常")
  56. except Exception as e:
  57. logger.error(f"task_id = {task_id} , error = {e}")
  58. sql = "UPDATE decode_record SET task_status = 3, WHERE task_id = %s"
  59. mysql.execute(sql, (task_id))
  60. logger.info(f"task_id = {task_id} ,任务异常")
  61. raise {"task_id": task_id, "error": '任务异常'}