1. **依赖与导入路径修正
- 移除所有本地sys.path插入,统一使用绝对包导入
- 修复logger导入路径,统一从app.utils导入
- 重构intelligent_retry.py移除冗余导入
- 统一hrbp各模块的导入路径
2. **项目配置与构建
- 新增pyproject.toml配置文件,规范项目元数据与依赖声明
- 更新requirements.txt新增pyautogui、pywin32、pyperclip依赖
3. **业务模块重构
- 重构请假/加班审批模块的导入路径与内部逻辑
- 重命名审批结果描述(将"不通过"改为"驳回")
- 统一代码中的箭头符号(将→改为->)
- 新增请假表单解析模块、钉钉通知模块
- 重构加班/请假校验模块的报告生成逻辑,统一报告格式与推送流程
4. **测试与文档
- 新增端到端测试脚本与统一测试运行器
- 完善README.md文档,补充项目结构、功能说明与使用方法
- 新增钉钉消息推送工具文档
- 新增测试用例与辅助工具
5. **其他优化
- 修复common/parser.py中的正则表达式兼容单引号的HTML属性
- 新增session_manager.py移除冗余导入与路径修正
- 新增OA审批报告生成逻辑优化,支持分组统计与详细清单输出
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
@Module: hrbp.utils
|
||
@FileName: utils.py
|
||
@Author: 小布丁
|
||
@CreateTime: 2026-08-05 21:30
|
||
@Description: 项目级通用工具(重试装饰器)
|
||
@Version: 1.1.0
|
||
"""
|
||
|
||
import functools
|
||
import random
|
||
import time
|
||
from typing import Callable
|
||
|
||
from app.utils.logger import get_logger
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
|
||
class RetryExhaustedError(Exception):
|
||
"""重试耗尽后抛出的异常,保留原始异常链。"""
|
||
pass
|
||
|
||
|
||
def retry(max_attempts: int = 3) -> Callable:
|
||
"""
|
||
重试装饰器:内置指数退避 + 随机抖动,只暴露重试次数。
|
||
|
||
Args:
|
||
max_attempts: 最大尝试次数(含首次执行),默认 3,必须 >= 1。
|
||
|
||
内部行为(对调用方透明):
|
||
- 捕获所有 Exception;
|
||
- 延迟策略:第 n 次重试等待 1.0 * 2^(n-1) 秒,并乘以 [0.5, 1.5] 随机因子;
|
||
- 全部失败后抛出 RetryExhaustedError,可通过 __cause__ 获取原始异常。
|
||
"""
|
||
if max_attempts < 1:
|
||
raise ValueError("max_attempts must be >= 1")
|
||
|
||
def decorator(func: Callable) -> Callable:
|
||
@functools.wraps(func)
|
||
def wrapper(*args, **kwargs):
|
||
last_exc = None
|
||
base_delay = 1.0
|
||
backoff = 2.0
|
||
|
||
for attempt in range(1, max_attempts + 1):
|
||
try:
|
||
return func(*args, **kwargs)
|
||
except Exception as e:
|
||
last_exc = e
|
||
if attempt >= max_attempts:
|
||
logger.error(
|
||
"%s 连续 %d 次尝试失败,放弃并记录: %s",
|
||
func.__name__, max_attempts, e,
|
||
)
|
||
break
|
||
|
||
# 计算退避 + 抖动
|
||
delay = base_delay * (backoff ** (attempt - 1))
|
||
delay *= random.uniform(0.5, 1.5)
|
||
|
||
logger.warning(
|
||
"%s 第 %d 次执行失败: %s,%.2f 秒后重试",
|
||
func.__name__, attempt, e, delay,
|
||
)
|
||
time.sleep(delay)
|
||
|
||
raise RetryExhaustedError(
|
||
f"{func.__name__} failed after {max_attempts} attempts"
|
||
) from last_exc
|
||
|
||
return wrapper
|
||
return decorator
|