197 lines
8.0 KiB
Python
197 lines
8.0 KiB
Python
import asyncio
|
||
from typing import Dict, Any
|
||
EXTERNAL_CALLBACK_URL = "http://roh.vwfawedl.mobi:9001/AGVService/ContainerSendBackRequest" # 生产线到毛坯库任务
|
||
|
||
|
||
async def test1(a: int, b: int) -> int:
|
||
return {"name": a + b}
|
||
|
||
def name1():
|
||
print('=====')
|
||
|
||
async def validate_task_condition(function_args: Dict[str, Any]) -> Dict[str, Any]:
|
||
"""
|
||
任务状态验证功能,用于验证关联任务的状态
|
||
|
||
Args:
|
||
function_args: 包含以下参数的字典
|
||
- task_code: 任务代码,用于查询外部任务记录
|
||
- task_type: 任务类型,用于判断验证逻辑
|
||
- end_node: 终点节点(仅对GT类型任务需要)
|
||
|
||
Returns:
|
||
Dict[str, Any]: 验证结果
|
||
"""
|
||
from services.external_task_record_service import ExternalTaskRecordService
|
||
from services.task_record_service import TaskRecordService
|
||
from data.session import get_async_session
|
||
from data.models.operate_point_layer import OperatePointLayer
|
||
from data.enum.task_record_enum import TaskStatus
|
||
from sqlalchemy import select
|
||
from utils.logger import get_logger
|
||
|
||
logger = get_logger("scripts.user_save.test1")
|
||
# print(function_args, "=========================")
|
||
try:
|
||
# 获取参数
|
||
task_code = function_args.get('task_code')
|
||
task_type = function_args.get('task_type')
|
||
end_node = function_args.get('end_node')
|
||
|
||
if not task_code:
|
||
return {
|
||
"success": False,
|
||
"message": "task_code参数为空"
|
||
}
|
||
|
||
if not task_type:
|
||
return {
|
||
"success": False,
|
||
"message": "task_type参数为空"
|
||
}
|
||
|
||
# 定义任务类型模板映射
|
||
TASK_TYPE_TEMPLATE_MAPPING = {
|
||
"GG2MP": "GG",
|
||
"GGFK2MP": "GG",
|
||
"GT2MP": "GT",
|
||
"GTFK2MP": "GT",
|
||
"ZG2MP": "ZG",
|
||
"QZ2MP": "QZ",
|
||
"LG2MP": "LG",
|
||
"PHZ2MP": "PHZ",
|
||
"MP2GG": "GG",
|
||
"MP2GGFK": "GG",
|
||
"MP2GT": "GT",
|
||
"MP2GTFK": "GT",
|
||
"MP2ZG": "ZG",
|
||
"MP2QZ": "QZ",
|
||
"MP2LG": "LG",
|
||
"MP2PHZ": "PHZ"
|
||
}
|
||
|
||
# 根据TaskCode查询external_task_record表获取task_record_id
|
||
external_records = await ExternalTaskRecordService.get_task_records_by_task_code(task_code)
|
||
logger.info(f"系统相关记录: {external_records}")
|
||
|
||
if not external_records:
|
||
return {
|
||
"success": False,
|
||
"message": f"找不到TaskCode={task_code}对应的叫料任务记录,无法监控叫料任务状态"
|
||
}
|
||
|
||
# 获取最新的记录
|
||
latest_record = max(external_records, key=lambda x: x.created_at)
|
||
|
||
if not latest_record.task_record_id:
|
||
return {
|
||
"success": False,
|
||
"message": "叫料任务记录对应关键字task_record_id值为空"
|
||
}
|
||
|
||
# 获取TaskType对应的模板类型
|
||
template_type = TASK_TYPE_TEMPLATE_MAPPING.get(task_type, "")
|
||
logger.info(f"TaskCode={task_code}, TaskType={task_type}, TemplateType={template_type}")
|
||
|
||
while True:
|
||
try:
|
||
async with get_async_session() as session:
|
||
# 查询end_node对应的库位锁定状态
|
||
stmt = select(OperatePointLayer).where(
|
||
OperatePointLayer.layer_name == end_node,
|
||
OperatePointLayer.is_deleted == False
|
||
).limit(1)
|
||
result = await session.execute(stmt)
|
||
end_layer = result.scalar_one_or_none()
|
||
task_detail_result = await TaskRecordService.get_task_record_detail(
|
||
latest_record.task_record_id)
|
||
task_detail = task_detail_result.get("data", {})
|
||
task_status = task_detail.get("status", "")
|
||
if task_status == TaskStatus.CANCELED:
|
||
return {
|
||
"success": True,
|
||
"message": f"任务被取消: TaskCode={task_code}, Status={task_status}"
|
||
}
|
||
if end_layer:
|
||
if not end_layer.is_locked:
|
||
return {
|
||
"success": True,
|
||
"message": f"任务验证通过,end_node库位已解锁: {end_node}"
|
||
}
|
||
else:
|
||
logger.info(f"任务,end_node库位被锁定,等待解锁: TaskCode={task_code}, end_node={end_node}, locked_by={end_layer.locked_by}")
|
||
await asyncio.sleep(2) # 等待2秒后重试
|
||
else:
|
||
logger.warning(f"任务,未找到end_node对应的库位,继续执行: TaskCode={task_code}, end_node={end_node}")
|
||
return {
|
||
"success": True,
|
||
"message": f"任务验证通过,未找到对应库位,继续执行: {end_node}"
|
||
}
|
||
|
||
except Exception as e:
|
||
logger.error(f"任务,检查end_node库位锁定状态时出现异常: {str(e)}, TaskCode={task_code}, end_node={end_node}")
|
||
await asyncio.sleep(2) # 等待2秒后重试
|
||
|
||
|
||
except Exception as e:
|
||
logger.error(f"任务状态验证异常: {str(e)}")
|
||
return {
|
||
"success": False,
|
||
"message": f"任务状态验证异常: {str(e)}"
|
||
}
|
||
|
||
|
||
async def call_external_callback(req_code: str, task_type: str, arrival_user: str = "000307" ) -> dict:
|
||
"""
|
||
调用外部回调接口
|
||
|
||
Args:
|
||
req_code: 到货编号(ReqCode)
|
||
task_type: 任务类型
|
||
arrival_user: 到货用户,固定值 000307
|
||
|
||
Returns:
|
||
bool: 调用是否成功(返回result为0)
|
||
"""
|
||
arrival_no = req_code
|
||
import aiohttp
|
||
from utils.logger import get_logger
|
||
logger = get_logger("scripts.user_save.test1")
|
||
if task_type not in ["GTFK2MP", "GGFK2MP"]:
|
||
return {"message": "不是返空类型 不需要过账"}
|
||
|
||
payload = {
|
||
"arrival_no": arrival_no,
|
||
"arrival_user": arrival_user
|
||
}
|
||
|
||
max_retries = 100 # 最大重试次数,防止无限循环
|
||
retry_count = 0
|
||
|
||
while retry_count < max_retries:
|
||
try:
|
||
async with aiohttp.ClientSession() as sessions:
|
||
async with sessions.post(EXTERNAL_CALLBACK_URL, json=payload) as response:
|
||
result = await response.json()
|
||
logger.info(f"外部接口调用响应: {result}, arrival_no={arrival_no}, 重试次数={retry_count}")
|
||
|
||
# 检查响应结果
|
||
if result.get("result") == "0":
|
||
logger.info(f"外部接口调用成功: arrival_no={arrival_no}, 总重试次数={retry_count}")
|
||
return {"message": "空托盘过账成功!"}
|
||
elif result.get("result") == "1":
|
||
logger.info(f"外部接口返回result=1,继续重试: arrival_no={arrival_no}, 重试次数={retry_count}")
|
||
retry_count += 1
|
||
await asyncio.sleep(5) # 等待5秒后重试
|
||
else:
|
||
logger.error(f"外部接口返回异常结果: {result}, arrival_no={arrival_no}")
|
||
retry_count += 1
|
||
await asyncio.sleep(5)
|
||
|
||
except Exception as e:
|
||
logger.error(f"调用外部接口异常: {str(e)}, arrival_no={arrival_no}, 重试次数={retry_count}")
|
||
retry_count += 1
|
||
await asyncio.sleep(5) # 等待5秒后重试
|
||
|
||
logger.error(f"外部接口调用失败,已达到最大重试次数: arrival_no={arrival_no}, 最大重试次数={max_retries}")
|
||
return {"message": "空托盘过账失败,重新尝试次数达到最大值"} |