fix: 推送网络请求加重试,避免瞬时失败漏推

mDNS(.local)域名解析基于组播UDP,偶发丢包会导致整轮推送直接抛异常跳过。
_request() 对连接类异常加重试(2次/间隔2s);画廊清理列表拉取失败也不再
阻塞后续 /upload,清理是锦上添花而非推送必要条件。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 10:55:15 +08:00
parent 2121b1eff4
commit b32007483a
2 changed files with 24 additions and 8 deletions

View File

@@ -6,21 +6,31 @@
http://<EPD_HOST>/ 调试同一套接口)。
"""
import json
import time
import urllib.parse
import urllib.request
import urllib.error
import config
# 局域网 mDNS.local域名解析基于组播 UDP偶发瞬时失败正常重试即可恢复。
RETRY_TIMES = 2
RETRY_DELAY = 2
def _request(method, path, headers=None, body=None):
url = f"http://{config.EPD_HOST}{path}"
req = urllib.request.Request(url, data=body, headers=headers or {}, method=method)
try:
with urllib.request.urlopen(req, timeout=30) as resp:
return resp.read().decode("utf-8", "ignore")
except urllib.error.HTTPError as e:
raise RuntimeError(f"HTTP {e.code}: {e.read().decode('utf-8', 'ignore')}") from e
for attempt in range(RETRY_TIMES + 1):
try:
with urllib.request.urlopen(req, timeout=30) as resp:
return resp.read().decode("utf-8", "ignore")
except urllib.error.HTTPError as e:
raise RuntimeError(f"HTTP {e.code}: {e.read().decode('utf-8', 'ignore')}") from e
except urllib.error.URLError:
if attempt == RETRY_TIMES:
raise
time.sleep(RETRY_DELAY)
def _list_gallery_names():
@@ -29,8 +39,14 @@ def _list_gallery_names():
def _clear_gallery():
"""设备画廊按追加保存,用完即删,使其始终只留最新一张。单张删除失败不影响后续上传。"""
for name in _list_gallery_names():
"""设备画廊按追加保存,用完即删,使其始终只留最新一张。
清理是锦上添花,不是推送的必要条件——拉列表或单张删除失败都不能阻塞后续上传。"""
try:
names = _list_gallery_names()
except Exception as e:
print(f"[推送] 拉取画廊列表失败,跳过本次清理:{e}")
return
for name in names:
try:
_request("POST", f"/delete_image?name={urllib.parse.quote(name)}")
except Exception as e: