2025-02-19 11:37:22 +08:00
|
|
|
|
import multiprocessing
|
2025-01-10 14:30:35 +08:00
|
|
|
|
import os
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
from enum import Enum
|
|
|
|
|
from typing import Any
|
2025-02-19 11:37:22 +08:00
|
|
|
|
from flask import Blueprint, g, current_app
|
2025-01-10 14:30:35 +08:00
|
|
|
|
from flask_app.general.format_change import download_file
|
|
|
|
|
from flask_app.routes.判断是否是招标文件 import judge_zbfile_exec
|
2025-01-14 17:10:38 +08:00
|
|
|
|
from flask_app.routes.utils import validate_and_setup_logger, create_response_normal, log_error_unique_id
|
2025-01-10 14:30:35 +08:00
|
|
|
|
|
|
|
|
|
judge_zbfile_bp = Blueprint('judge_zbfile', __name__)
|
|
|
|
|
class JudgeResult(Enum):
|
|
|
|
|
ERROR = 1
|
|
|
|
|
YES = 2
|
|
|
|
|
NO = 3
|
|
|
|
|
@judge_zbfile_bp.route('/judge_zbfile', methods=['POST'])
|
|
|
|
|
@validate_and_setup_logger
|
2025-01-10 10:04:30 +08:00
|
|
|
|
# @require_connection_limit(timeout=30)
|
2025-02-07 15:27:24 +08:00
|
|
|
|
def judge_zbfile() -> Any: #判断是否是招标文件
|
2025-01-10 14:30:35 +08:00
|
|
|
|
"""
|
|
|
|
|
主函数,调用 wrapper 并设置整个接口的超时时时间。如果超时返回默认值。
|
|
|
|
|
"""
|
|
|
|
|
logger = g.logger
|
|
|
|
|
file_url = g.file_url
|
|
|
|
|
output_folder = g.output_folder
|
2025-01-14 17:10:38 +08:00
|
|
|
|
unique_id = g.unique_id
|
2025-01-10 14:30:35 +08:00
|
|
|
|
result = [None] # 用于存储结果的可变对象
|
|
|
|
|
done = threading.Event() # 标志判断是否完成
|
2025-02-19 16:17:33 +08:00
|
|
|
|
pool = current_app.process_pool # 使用全局的进程池
|
2025-02-19 11:37:22 +08:00
|
|
|
|
def judge_zbfile_exec_sub(file_path):
|
2025-02-19 16:17:33 +08:00
|
|
|
|
# with multiprocessing.Pool(processes=1) as pool:
|
|
|
|
|
# result = pool.apply(
|
|
|
|
|
# judge_zbfile_exec, # 你的实际执行函数
|
|
|
|
|
# args=(file_path,)
|
|
|
|
|
# )
|
|
|
|
|
result = pool.apply(
|
|
|
|
|
judge_zbfile_exec, # 你的实际执行函数
|
2025-02-19 16:35:51 +08:00
|
|
|
|
args=(file_path,)
|
2025-02-19 16:17:33 +08:00
|
|
|
|
)
|
2025-02-19 11:37:22 +08:00
|
|
|
|
return result
|
2025-01-10 14:30:35 +08:00
|
|
|
|
def wrapper() -> None:
|
|
|
|
|
"""
|
|
|
|
|
包装整个 judge_zbfile 的函数逻辑
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
downloaded_filename = os.path.join(output_folder, "ztbfile")
|
2025-01-10 17:38:55 +08:00
|
|
|
|
logger.info(f"接收到的url:{file_url}")
|
2025-02-21 16:26:28 +08:00
|
|
|
|
downloaded_filepath, file_type = download_file(file_url, downloaded_filename,False) #判断是否为招标文件时不需要处理扫描型pdf
|
2025-01-10 14:30:35 +08:00
|
|
|
|
|
|
|
|
|
if not downloaded_filepath or file_type == 4:
|
2025-01-14 17:10:38 +08:00
|
|
|
|
log_error_unique_id(unique_id, 4)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
result[0] = JudgeResult.ERROR
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
logger.info(f"Local file path: {downloaded_filepath}")
|
|
|
|
|
|
|
|
|
|
# 调用实际的判断函数
|
2025-02-19 16:17:33 +08:00
|
|
|
|
judge_result = judge_zbfile_exec_sub(downloaded_filepath)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
judge = JudgeResult.YES if judge_result else JudgeResult.NO
|
|
|
|
|
|
|
|
|
|
end_time = time.time()
|
|
|
|
|
logger.info(f"接口实际耗时:{end_time - start_time:.2f} 秒")
|
|
|
|
|
result[0] = judge
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f'Exception occurred: {e}')
|
2025-01-14 17:10:38 +08:00
|
|
|
|
log_error_unique_id(unique_id, 4)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
result[0] = JudgeResult.ERROR
|
|
|
|
|
finally:
|
|
|
|
|
done.set()
|
|
|
|
|
|
|
|
|
|
# 启动后台线程执行 wrapper
|
|
|
|
|
thread = threading.Thread(target=wrapper, daemon=True)
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
# ****设置整个接口的超时时间,如果超时,会默认返回create_response_normal,但wrapper()仍继续执行!
|
|
|
|
|
timeout = 15
|
|
|
|
|
finished_in_time = done.wait(timeout)
|
|
|
|
|
if not finished_in_time:
|
|
|
|
|
logger.warning("整个接口执行超时,返回默认值 'yes'")
|
|
|
|
|
# 如果超时,返回默认响应
|
|
|
|
|
return create_response_normal(
|
|
|
|
|
message='判断是否为招标文件成功!',
|
|
|
|
|
status='success',
|
|
|
|
|
data='yes' # 默认返回值
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
return build_response(result[0], logger)
|
|
|
|
|
|
|
|
|
|
def build_response(judge_result: JudgeResult, logger) -> Any:
|
|
|
|
|
"""
|
|
|
|
|
根据 judge_result 构建响应
|
|
|
|
|
"""
|
|
|
|
|
if judge_result == JudgeResult.ERROR:
|
|
|
|
|
logger.error("下载地址不存在或不支持的文件类型!")
|
|
|
|
|
return create_response_normal(
|
|
|
|
|
message='下载地址不存在或不支持的文件类型!',
|
|
|
|
|
status='error',
|
|
|
|
|
data=''
|
|
|
|
|
)
|
|
|
|
|
elif judge_result == JudgeResult.YES:
|
2025-01-13 11:27:52 +08:00
|
|
|
|
logger.info("判断是否为招标文件成功!YES")
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
|
|
|
|
message='判断是否为招标文件成功!',
|
|
|
|
|
status='success',
|
|
|
|
|
data='yes'
|
|
|
|
|
)
|
|
|
|
|
elif judge_result == JudgeResult.NO:
|
2025-01-13 11:27:52 +08:00
|
|
|
|
logger.info("判断是否为招标文件成功!NO")
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
|
|
|
|
message='判断是否为招标文件成功!',
|
|
|
|
|
status='success',
|
|
|
|
|
data='no'
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
# 处理未知的结果
|
|
|
|
|
logger.error("服务器遇到不知名错误!")
|
|
|
|
|
return create_response_normal(
|
|
|
|
|
message='服务器遇到不知名错误!',
|
|
|
|
|
status='error',
|
|
|
|
|
data=''
|
|
|
|
|
)
|
|
|
|
|
|