Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ EMAIL_POLL_TIMEOUT=300
AUTO_CHECK_INTERVAL=300 # 巡检间隔(秒),默认 5 分钟
AUTO_CHECK_THRESHOLD=10 # 额度低于此百分比触发轮转,默认 10%
AUTO_CHECK_MIN_LOW=2 # 至少几个账号低于阈值才触发,默认 2
AUTO_CHECK_RETRY_ADD_PHONE=true # 是否自动重试 add_phone(手机号验证)
AUTO_CHECK_ADD_PHONE_MAX_RETRIES=3 # add_phone 最大自动重试次数
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ cp .env.example .env
| `AUTO_CHECK_THRESHOLD` | 额度低于此百分比触发轮转 | 否(默认 `10`) |
| `AUTO_CHECK_INTERVAL` | 巡检间隔(秒) | 否(默认 `300`) |
| `AUTO_CHECK_MIN_LOW` | 至少几个账号低于阈值才触发 | 否(默认 `2`) |
| `AUTO_CHECK_RETRY_ADD_PHONE` | 是否自动重试 `add_phone`(手机号验证) | 否(默认 `true`) |
| `AUTO_CHECK_ADD_PHONE_MAX_RETRIES` | `add_phone` 最大自动重试次数 | 否(默认 `3`) |

## 配置面板分区

Expand Down Expand Up @@ -146,6 +148,8 @@ PLAYWRIGHT_PROXY_URL=http://username:password@host.docker.internal:1080

```env
AUTO_CHECK_INTERVAL=300 # 5 分钟
AUTO_CHECK_RETRY_ADD_PHONE=true
AUTO_CHECK_ADD_PHONE_MAX_RETRIES=3
```

Windows / macOS 下也会按 UTF-8 正常读取。
Expand Down
41 changes: 38 additions & 3 deletions src/autoteam/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class SourceConfig(BaseModel):
"AUTO_CHECK_INTERVAL",
"AUTO_CHECK_THRESHOLD",
"AUTO_CHECK_MIN_LOW",
"AUTO_CHECK_RETRY_ADD_PHONE",
"AUTO_CHECK_ADD_PHONE_MAX_RETRIES",
"PLAYWRIGHT_PROXY_URL",
"PLAYWRIGHT_PROXY_SERVER",
"PLAYWRIGHT_PROXY_USERNAME",
Expand Down Expand Up @@ -577,11 +579,19 @@ def _sync_runtime_globals():
return

try:
from autoteam.config import AUTO_CHECK_INTERVAL, AUTO_CHECK_MIN_LOW, AUTO_CHECK_THRESHOLD
from autoteam.config import (
AUTO_CHECK_ADD_PHONE_MAX_RETRIES,
AUTO_CHECK_INTERVAL,
AUTO_CHECK_MIN_LOW,
AUTO_CHECK_RETRY_ADD_PHONE,
AUTO_CHECK_THRESHOLD,
)

auto_check_config["interval"] = AUTO_CHECK_INTERVAL
auto_check_config["threshold"] = AUTO_CHECK_THRESHOLD
auto_check_config["min_low"] = AUTO_CHECK_MIN_LOW
auto_check_config["retry_add_phone"] = AUTO_CHECK_RETRY_ADD_PHONE
auto_check_config["add_phone_max_retries"] = AUTO_CHECK_ADD_PHONE_MAX_RETRIES
if auto_check_restart is not None:
auto_check_restart.set()
except Exception:
Expand Down Expand Up @@ -2341,12 +2351,18 @@ def get_task(task_id: str):
# 后台自动巡检
# ---------------------------------------------------------------------------

from autoteam.config import (
AUTO_CHECK_ADD_PHONE_MAX_RETRIES as _DEFAULT_ADD_PHONE_MAX_RETRIES,
)
from autoteam.config import (
AUTO_CHECK_INTERVAL as _DEFAULT_INTERVAL,
)
from autoteam.config import (
AUTO_CHECK_MIN_LOW as _DEFAULT_MIN_LOW,
)
from autoteam.config import (
AUTO_CHECK_RETRY_ADD_PHONE as _DEFAULT_RETRY_ADD_PHONE,
)
from autoteam.config import (
AUTO_CHECK_THRESHOLD as _DEFAULT_THRESHOLD,
)
Expand All @@ -2356,6 +2372,8 @@ def get_task(task_id: str):
"interval": _DEFAULT_INTERVAL,
"threshold": _DEFAULT_THRESHOLD,
"min_low": _DEFAULT_MIN_LOW,
"retry_add_phone": _DEFAULT_RETRY_ADD_PHONE,
"add_phone_max_retries": _DEFAULT_ADD_PHONE_MAX_RETRIES,
}
_auto_check_stop = threading.Event()
_auto_check_restart = threading.Event() # 配置变更时通知线程重启
Expand Down Expand Up @@ -2837,22 +2855,35 @@ class AutoCheckConfig(BaseModel):
interval: int = 300 # 巡检间隔(秒)
threshold: int = 10 # 额度阈值(%)
min_low: int = 2 # 触发轮转的最少账号数
retry_add_phone: bool = True # 是否自动重试 add_phone
add_phone_max_retries: int = 3 # add_phone 最大自动重试次数


def _normalized_auto_check_config(cfg: AutoCheckConfig | dict[str, int]) -> dict[str, int]:
def _normalized_auto_check_config(cfg: AutoCheckConfig | dict[str, object]) -> dict[str, int | bool]:
if isinstance(cfg, AutoCheckConfig):
interval = cfg.interval
threshold = cfg.threshold
min_low = cfg.min_low
retry_add_phone = cfg.retry_add_phone
add_phone_max_retries = cfg.add_phone_max_retries
else:
interval = cfg.get("interval", _auto_check_config.get("interval", _DEFAULT_INTERVAL))
threshold = cfg.get("threshold", _auto_check_config.get("threshold", _DEFAULT_THRESHOLD))
min_low = cfg.get("min_low", _auto_check_config.get("min_low", _DEFAULT_MIN_LOW))
retry_add_phone = cfg.get(
"retry_add_phone", _auto_check_config.get("retry_add_phone", _DEFAULT_RETRY_ADD_PHONE)
)
add_phone_max_retries = cfg.get(
"add_phone_max_retries",
_auto_check_config.get("add_phone_max_retries", _DEFAULT_ADD_PHONE_MAX_RETRIES),
)

return {
"interval": max(60, int(interval)),
"threshold": max(1, min(100, int(threshold))),
"min_low": max(1, int(min_low)),
"retry_add_phone": bool(retry_add_phone),
"add_phone_max_retries": max(1, int(add_phone_max_retries)),
}


Expand All @@ -2874,6 +2905,8 @@ def set_auto_check_config(cfg: AutoCheckConfig):
"AUTO_CHECK_INTERVAL": str(normalized["interval"]),
"AUTO_CHECK_THRESHOLD": str(normalized["threshold"]),
"AUTO_CHECK_MIN_LOW": str(normalized["min_low"]),
"AUTO_CHECK_RETRY_ADD_PHONE": "true" if normalized["retry_add_phone"] else "false",
"AUTO_CHECK_ADD_PHONE_MAX_RETRIES": str(normalized["add_phone_max_retries"]),
}
for key, value in persisted.items():
os.environ[key] = value
Expand All @@ -2882,10 +2915,12 @@ def set_auto_check_config(cfg: AutoCheckConfig):
_sync_runtime_env_reload_state()
_auto_check_restart.set() # 唤醒巡检线程,立即应用新配置
logger.info(
"[巡检] 配置已更新并持久化: 间隔=%ds 阈值=%d%% 触发=%d个",
"[巡检] 配置已更新并持久化: 间隔=%ds 阈值=%d%% 触发=%d个 add_phone自动重试=%s 最大重试=%d",
_auto_check_config["interval"],
_auto_check_config["threshold"],
_auto_check_config["min_low"],
"开" if _auto_check_config["retry_add_phone"] else "关",
_auto_check_config["add_phone_max_retries"],
)
return _auto_check_config.copy()

Expand Down
2 changes: 2 additions & 0 deletions src/autoteam/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def _normalize_chatgpt_api_transport(value: str) -> str:
AUTO_CHECK_INTERVAL = _get_int_env("AUTO_CHECK_INTERVAL", 300) # 巡检间隔(秒),默认 5 分钟
AUTO_CHECK_THRESHOLD = _get_int_env("AUTO_CHECK_THRESHOLD", 10) # 额度低于此百分比触发轮转,默认 10%
AUTO_CHECK_MIN_LOW = _get_int_env("AUTO_CHECK_MIN_LOW", 2) # 至少几个账号低于阈值才触发,默认 2
AUTO_CHECK_RETRY_ADD_PHONE = _get_bool_env("AUTO_CHECK_RETRY_ADD_PHONE", True) # 是否自动重试 add_phone
AUTO_CHECK_ADD_PHONE_MAX_RETRIES = _get_int_env("AUTO_CHECK_ADD_PHONE_MAX_RETRIES", 3) # add_phone 最大自动重试次数

# Playwright 代理配置
PLAYWRIGHT_PROXY_URL = os.environ.get("PLAYWRIGHT_PROXY_URL", "").strip()
Expand Down
Loading
Loading