|
|
@@ -35,12 +35,13 @@ def _build_success_response(task_id: str) -> Dict[str, Any]:
|
|
|
}
|
|
|
|
|
|
|
|
|
-def _create_workflow_task(scene: SceneEnum) -> Optional[WorkflowTask]:
|
|
|
+def _create_workflow_task(scene: SceneEnum, content_type: ContentTypeEnum) -> Optional[WorkflowTask]:
|
|
|
"""创建工作流任务"""
|
|
|
try:
|
|
|
task = WorkflowTask.create_task(
|
|
|
scene=scene,
|
|
|
capability=CapabilityEnum.DECODE,
|
|
|
+ content_type=content_type,
|
|
|
root_task_id=''
|
|
|
)
|
|
|
logger.info(f"创建解构任务成功,task_id: {task.task_id}")
|
|
|
@@ -64,14 +65,14 @@ def _initialize_task_result(task_id: str, content) -> Optional[WorkflowDecodeTas
|
|
|
return None
|
|
|
|
|
|
|
|
|
-def _check_quota(scene: SceneEnum, capability: CapabilityEnum = CapabilityEnum.DECODE) -> bool:
|
|
|
+def _check_quota(scene: SceneEnum, capability: CapabilityEnum = CapabilityEnum.DECODE, content_type: ContentTypeEnum = ContentTypeEnum.TEXT) -> bool:
|
|
|
"""检查配额是否充足(并发安全版本)"""
|
|
|
try:
|
|
|
# 获取今天的日期,格式为 YYYYMMDD
|
|
|
quota_date = datetime.now().strftime('%Y%m%d')
|
|
|
scene_value = scene.value
|
|
|
capability_value = capability.value
|
|
|
-
|
|
|
+ content_type_value = content_type.value
|
|
|
# 使用事务和 SELECT FOR UPDATE 确保并发安全
|
|
|
pool = mysql.get_pool()
|
|
|
with pool.connection() as conn:
|
|
|
@@ -84,19 +85,19 @@ def _check_quota(scene: SceneEnum, capability: CapabilityEnum = CapabilityEnum.D
|
|
|
select_sql = """
|
|
|
SELECT quota_limit, used_count, locked
|
|
|
FROM workflow_daily_quota
|
|
|
- WHERE quota_date = %s AND scene = %s AND capability = %s
|
|
|
+ WHERE quota_date = %s AND scene = %s AND capability = %s AND content_type = %s
|
|
|
FOR UPDATE
|
|
|
"""
|
|
|
- cursor.execute(select_sql, (quota_date, scene_value, capability_value))
|
|
|
+ cursor.execute(select_sql, (quota_date, scene_value, capability_value, content_type_value))
|
|
|
quota_record = cursor.fetchone()
|
|
|
|
|
|
# 2. 如果没找到,创建一条新记录,quota_limit 默认为 10
|
|
|
if not quota_record:
|
|
|
insert_sql = """
|
|
|
- INSERT INTO workflow_daily_quota (scene, capability, quota_date, quota_limit, used_count, locked)
|
|
|
- VALUES (%s, %s, %s, %s, %s, %s)
|
|
|
+ INSERT INTO workflow_daily_quota (scene, capability, content_type, quota_date, quota_limit, used_count, locked)
|
|
|
+ VALUES (%s, %s, %s, %s, %s, %s, %s)
|
|
|
"""
|
|
|
- cursor.execute(insert_sql, (scene_value, capability_value, quota_date, 10, 0, 0))
|
|
|
+ cursor.execute(insert_sql, (scene_value, capability_value, content_type_value, quota_date, 10, 0, 0))
|
|
|
quota_limit = 10
|
|
|
current_used_count = 0
|
|
|
is_locked = 0
|
|
|
@@ -117,9 +118,10 @@ def _check_quota(scene: SceneEnum, capability: CapabilityEnum = CapabilityEnum.D
|
|
|
FROM workflow_task
|
|
|
WHERE scene = %s
|
|
|
AND capability = %s
|
|
|
+ AND content_type = %s
|
|
|
AND DATE(created_time) = CURDATE()
|
|
|
"""
|
|
|
- cursor.execute(count_sql, (scene_value, capability_value))
|
|
|
+ cursor.execute(count_sql, (scene_value, capability_value, content_type_value))
|
|
|
count_result = cursor.fetchone()
|
|
|
actual_used_count = count_result.get('task_count', 0) if count_result else 0
|
|
|
|
|
|
@@ -148,14 +150,14 @@ def decode_topic(param: DecodeContentParam) -> Dict[str, Any]:
|
|
|
"""选题解构方法"""
|
|
|
try:
|
|
|
# 前置配额检查,用于超出每天解构次数时,直接返回错误
|
|
|
- if not _check_quota(param.scene, CapabilityEnum.DECODE):
|
|
|
+ if not _check_quota(param.scene, CapabilityEnum.DECODE, param.content_type):
|
|
|
return _build_error_response(
|
|
|
ERROR_CODE_FAILED,
|
|
|
"配额不足"
|
|
|
)
|
|
|
|
|
|
# 步骤1: 创建工作流task任务
|
|
|
- task = _create_workflow_task(param.scene)
|
|
|
+ task = _create_workflow_task(param.scene, param.content_type)
|
|
|
if not task or not task.task_id:
|
|
|
return _build_error_response(
|
|
|
ERROR_CODE_TASK_CREATE_FAILED,
|
|
|
@@ -171,10 +173,11 @@ def decode_topic(param: DecodeContentParam) -> Dict[str, Any]:
|
|
|
)
|
|
|
|
|
|
# 步骤3: 触发解构工作流
|
|
|
- if not _trigger_topic_decode_workflow(task.task_id):
|
|
|
+ trigger_result = _trigger_topic_decode_workflow(task.task_id)
|
|
|
+ if trigger_result.get("code") != ERROR_CODE_SUCCESS:
|
|
|
return _build_error_response(
|
|
|
ERROR_CODE_FAILED,
|
|
|
- "发起解构任务失败"
|
|
|
+ trigger_result.get("reason") or "发起解构任务失败"
|
|
|
)
|
|
|
|
|
|
# 所有步骤成功
|