This commit is contained in:
2026-08-26 13:33:21 +08:00
parent d6656fedc5
commit 48dd28a796
5 changed files with 194 additions and 26 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ REM 每次升级改 APP_VERSION,避免与正在运行的旧版本目录冲突
REM 使用:把整个文件夹拷到目标机器(或压缩成 zip 发送),
REM 双击文件夹里的 Boss直聘AI投递-%APP_VERSION%.exe 运行。
REM ============================================================
set APP_VERSION=1.4
set APP_VERSION=1.5
echo [1/2] 安装打包依赖...
pip install pyinstaller PySide6 --quiet
+19 -8
View File
@@ -21,6 +21,7 @@ from loguru import logger
from .cdp_lite import LiteCDP, CDPSession
from .manager import BrowserManager
from .. import outbox
from ..ai import AIClient
from ..server_api import ServerAPI
@@ -427,7 +428,10 @@ class Applicant:
def _handle_card(self, card: JobCard, salary_min, salary_max,
min_score, delay_range) -> int:
try:
# 云端去重只认实际投递过的(chat/deliver);
# 被跳过的职位不算已投递,下次出现会重新评估
if self.server.check_applied(card.job_id):
logger.debug(f"已投递过,跳过(不计入跳过数):{card.company} - {card.title}")
return 0
except Exception:
pass
@@ -570,8 +574,7 @@ class Applicant:
# 服务端上报
# ------------------------------------------------------------------
def _report_apply(self, card: JobCard, ai_score: int, ai_reason: str):
try:
self.server.report_application(self.session_id, {
body = {
"job_id": card.job_id,
"job_title": card.title,
"company": card.company,
@@ -581,16 +584,20 @@ class Applicant:
"ai_score": ai_score,
"ai_reason": ai_reason[:255] if ai_reason else None,
"jd_text": card.jd_text or None,
})
}
# 先落盘兜底,发送成功才删除;失败留后台补发
entry_id = outbox.enqueue(self.session_id, body, applied_delta=1)
try:
self.server.report_application(self.session_id, body)
outbox.ack(entry_id)
self.server.update_session_stats(self.session_id, applied_delta=1, skipped_delta=0)
self.stats["applied"] += 1
logger.success(f"[{self.stats['applied']}] 投递成功:{card.company} - {card.title}")
except Exception as e:
logger.warning(f"上报投递记录失败:{e}")
logger.warning(f"上报投递记录失败,已入本地 outbox 后台补发:{e}")
def _report_skip(self, card: JobCard, reason: str, ai_score: int = -1, ai_reason: str = ""):
try:
self.server.report_application(self.session_id, {
body = {
"job_id": card.job_id,
"job_title": card.title,
"company": card.company,
@@ -601,9 +608,13 @@ class Applicant:
"ai_reason": ai_reason[:255] if ai_reason else None,
"skip_reason": reason,
"jd_text": card.jd_text or None,
})
}
entry_id = outbox.enqueue(self.session_id, body, skipped_delta=1)
try:
self.server.report_application(self.session_id, body)
outbox.ack(entry_id)
self.server.update_session_stats(self.session_id, applied_delta=0, skipped_delta=1)
self.stats["skipped"] += 1
logger.info(f"跳过({reason}):{card.company} - {card.title}")
except Exception as e:
logger.debug(f"上报跳过记录失败:{e}")
logger.debug(f"上报跳过记录失败,已入本地 outbox 后台补发:{e}")
+148
View File
@@ -0,0 +1,148 @@
"""
本地投递 outbox(存储转发兜底)
====================================
上报先落盘再发送,防止网络抖动/云端不可用导致记录丢失:
- enqueue() 上报前先写盘,返回 entry_id
- ack() 发送成功后立即删除(常态下队列≈空,不积压)
- flush() 补发一轮:由心跳线程每 30s 驱动一次;按入队顺序发送,
网络未恢复时本轮立即停止,等下一轮,不做集中无效重试
防扩张约束:
- MAX_ITEMS 硬上限 1000 条,超出丢弃最旧并告警
- RETENTION_DAYS 保留期 7 天(与日志保留一致),过期丢弃
- 服务端按 user+job_id 幂等更新,重复补发不会产生重复记录
"""
import json
import threading
import time
import uuid
from pathlib import Path
import requests
from loguru import logger
OUTBOX_FILE = Path("client/data/outbox.json")
MAX_ITEMS = 1000
RETENTION_DAYS = 7
_lock = threading.Lock()
def _load() -> list[dict]:
if not OUTBOX_FILE.exists():
return []
try:
return json.loads(OUTBOX_FILE.read_text(encoding="utf-8")) or []
except Exception:
logger.warning("outbox 文件读取失败,按空队列处理")
return []
def _save(items: list[dict]) -> None:
OUTBOX_FILE.parent.mkdir(parents=True, exist_ok=True)
tmp = OUTBOX_FILE.with_suffix(".tmp")
tmp.write_text(json.dumps(items, ensure_ascii=False), encoding="utf-8")
tmp.replace(OUTBOX_FILE)
def enqueue(session_id: int | None, body: dict,
applied_delta: int = 0, skipped_delta: int = 0) -> str:
"""上报前写盘兜底,返回 entry_id;发送成功后调用 ack 删除"""
entry_id = uuid.uuid4().hex
with _lock:
items = _load()
items.append({
"id": entry_id,
"created_at": time.time(),
"session_id": session_id,
"body": body,
"applied_delta": applied_delta,
"skipped_delta": skipped_delta,
})
if len(items) > MAX_ITEMS:
dropped = len(items) - MAX_ITEMS
logger.warning(f"outbox 超 {MAX_ITEMS} 条上限,丢弃最旧 {dropped} 条")
items = items[-MAX_ITEMS:]
_save(items)
return entry_id
def ack(entry_id: str) -> None:
"""发送成功后从 outbox 删除"""
with _lock:
_save([i for i in _load() if i.get("id") != entry_id])
def pending_count() -> int:
with _lock:
return len(_load())
def _send_one(server_api, item: dict) -> str:
"""补发单条。返回:sent=成功 / keep=网络类失败保留 / drop=业务拒绝丢弃"""
body, sid = item["body"], item.get("session_id")
try:
server_api.report_application(sid, body)
except requests.HTTPError as e:
status = getattr(getattr(e, "response", None), "status_code", 0)
if 400 <= status < 500 and status not in (409, 429):
# 业务拒绝(422/401 等):去掉 session_id 再试一次,
# 仍被拒则为毒丸消息,丢弃避免卡住补发循环
try:
server_api.report_application(None, body)
except requests.HTTPError:
logger.warning(f"outbox 条目被服务端拒绝,丢弃:{body.get('job_id')}")
return "drop"
except Exception:
return "keep"
else:
return "keep"
except Exception:
return "keep"
# 补发成功:会话统计也补上(best-effort)
if item.get("session_id") and (item.get("applied_delta") or item.get("skipped_delta")):
try:
server_api.update_session_stats(
item["session_id"],
item.get("applied_delta", 0), item.get("skipped_delta", 0))
except Exception as e:
logger.debug(f"outbox 补发会话统计失败:{e}")
return "sent"
def flush(server_api) -> int:
"""补发一轮,返回成功条数。网络未恢复时本轮提前结束等下一轮"""
sent = 0
with _lock:
items = _load()
if not items:
return 0
# 保留期清理
cutoff = time.time() - RETENTION_DAYS * 86400
expired = [i for i in items if i.get("created_at", 0) < cutoff]
if expired:
logger.warning(f"outbox 超保留期({RETENTION_DAYS} 天),丢弃 {len(expired)} 条")
items = [i for i in items if i.get("created_at", 0) >= cutoff]
remaining: list[dict] = []
stopped = False
for it in items:
if stopped:
remaining.append(it)
continue
result = _send_one(server_api, it)
if result == "sent":
sent += 1
elif result == "drop":
continue
else: # keep:网络未恢复,本条及后续全部保留,等下一轮
remaining.append(it)
stopped = True
_save(remaining)
if sent:
logger.info(f"outbox 补发成功 {sent} 条,剩余 {pending_count()} 条")
return sent
+6
View File
@@ -14,6 +14,7 @@ from pydantic import BaseModel
from loguru import logger
from ..config import client_settings
from .. import outbox
from ..server_api import ServerAPI
from ..ai import AIClient
from ..fingerprint import get_machine_summary
@@ -64,6 +65,11 @@ class AppState:
logger.info("已退出登录,心跳停止")
break
self.server_api.heartbeat()
# 心跳每轮顺便补发 outbox(网络恢复后自动补齐丢失记录)
try:
outbox.flush(self.server_api)
except Exception as e:
logger.debug(f"outbox 补发轮异常:{e}")
time.sleep(client_settings.HEARTBEAT_INTERVAL)
+3
View File
@@ -37,6 +37,9 @@ def report_application(
existing.ai_score = req.ai_score
existing.ai_reason = req.ai_reason
existing.skip_reason = req.skip_reason
# 重新评估上报刷新时间戳:时间倒序视图展示最新一次决策,
# 否则记录停留在首次上报时间,看起来像“漏报”
existing.applied_at = datetime.utcnow()
if req.jd_text:
existing.jd_text = req.jd_text
if req.greeting: