Enhance Chrome evaluator with improved error handling and retry mechanisms

- Added robust error handling for page processing, including checks for closed pages and HTTP status codes.
- Implemented retry logic for page loads and active tab checks to improve reliability.
- Enhanced logging throughout the process to capture detailed information about failures and successes.
- Preserved existing logic while ensuring better maintainability and robustness in the Chrome evaluator functions.
This commit is contained in:
yuanmengqi
2025-07-18 07:13:13 +00:00
parent 0fb625e4fd
commit fcaefe7bb4
2 changed files with 567 additions and 183 deletions

View File

@@ -21,6 +21,30 @@ def is_expected_active_tab(active_tab_info: Dict[str, str], rule: Dict[str, Any]
if not active_tab_info:
return 0.
# 添加重试机制
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(max_retries):
# 添加HTTP状态码检查
if 'status' in active_tab_info and active_tab_info['status'] >= 400:
logger.warning(f"Page load failed (attempt {attempt+1}/{max_retries}), HTTP status: {active_tab_info['status']}")
if attempt < max_retries - 1:
# 重试前刷新页面
logger.info(f"Refreshing page and retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
# 这里需要调用刷新页面的函数(实际实现取决于您的环境)
# 伪代码: refresh_active_tab()
# 然后重新获取 active_tab_info
# 伪代码: active_tab_info = get_active_tab_info()
continue
else:
logger.error(f"Page load failed after {max_retries} attempts")
return 0.
break # 如果状态码正常,跳出重试循环
match_type = rule['type']
if match_type == "url":
@@ -44,6 +68,26 @@ def is_expected_active_tab_approximate(active_tab_info: Dict[str, str], rule: Di
if not active_tab_info:
return 0.
# 添加相同的重试机制
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(max_retries):
if 'status' in active_tab_info and active_tab_info['status'] >= 400:
logger.warning(f"Page load failed (attempt {attempt+1}/{max_retries}), HTTP status: {active_tab_info['status']}")
if attempt < max_retries - 1:
logger.info(f"Refreshing page and retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
# 伪代码: refresh_active_tab()
# 伪代码: active_tab_info = get_active_tab_info()
continue
else:
logger.error(f"Page load failed after {max_retries} attempts")
return 0.
break
match_type = rule['type']
if match_type == "url":
@@ -74,11 +118,25 @@ def is_expected_url_pattern_match(result, rules) -> float:
if not result:
return 0.
if type(result) == dict:
result_url = result["url"]
logger.info("result url: {}".format(result_url))
else:
result_url = result
# 添加相同的重试机制
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(max_retries):
if isinstance(result, dict) and 'status' in result and result['status'] >= 400:
logger.warning(f"Page load failed (attempt {attempt+1}/{max_retries}), HTTP status: {result['status']}")
if attempt < max_retries - 1:
logger.info(f"Refreshing page and retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
# 伪代码: refresh_active_tab()
# 伪代码: result = get_active_tab_info()
continue
else:
logger.error(f"Page load failed after {max_retries} attempts")
return 0.
break
# expect_regex = re.compile(rules["expected"])
patterns = rules["expected"]
logger.info("expected_regex: {}".format(patterns))
@@ -220,6 +278,7 @@ import imagehash
from pathlib import Path
import typing
import time
def compare_pdf_images(pdf1_path: str, pdf2_path: str, **kwargs) -> float: