decodeTask.py 2.8 KB

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