fix(sync): 「全部历史」真的拉全部历史,自动同步不再每次静默失败

四个独立的 bug 叠在一起,表现为「只同步两天、没有进度」:

* 前端 `...(days ? { days } : {})` 把 days=0 当成未传。「全部历史」
  存的就是 0,请求体里根本没有 days,后端退回 7 天默认值。
* scheduler 用 `s[0]` 读 query_one 返回的 dict,抛 KeyError 后被
  per-account 的 except 吞掉。只要用户存过一次设置,每 30 分钟的
  自动同步就一次都没成功过——库里那 2 天全是手动点出来的。
* 增量同步查 `health_daily`(表其实叫 health_data),后台线程直接
  死掉,状态永远卡在 syncing,进度条不动。
* UI 完全不看 /sync 的返回值,rate_limited 时按钮点了没反应;轮询
  结束时又把 rate_limited 归进 else 分支报「同步完成」。

顺带:
* 日循环遇到 429 立即退避并保留已拉到的天数,而不是当成「跳过一天」
  继续往下捶 700 天——这正是之前限流死循环的来源之一。
* 定时循环显式传 SYNC_DAYS。历史范围按 UI 文案只描述手动全量同步,
  让半小时一次的 tick 重拉 730 天必然把限流撞得更深。
* 短同步逐天上报进度(原来每 5 天一次,7 天的同步全程停在 0)。
* /sync 路由重复解析 body,空 body 会 None.get 崩。
* 4 个 StubGarth 缺 configure(),7 个测试在此之前一直是红的。

新增 deploy/push.sh:NAS 只认密码,脚本开一个 ssh 复用连接,密码只
输一次,后面推送 / 重启 / 健康检查全走它。不碰 .env、.venv 和数据库。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
ericwyuan
2026-09-01 08:11:24 +08:00
parent 15fba8c25c
commit 41b7ae82e4
8 changed files with 283 additions and 20 deletions

View File

@@ -1021,8 +1021,14 @@ def sync_data(user_id, creds, days=None, client=None):
# -1 means "incremental sync": pick up from the latest date already in the
# local database rather than pulling a fixed window.
if days == -1:
row = query_one("SELECT MAX(date) FROM health_daily WHERE user_id = ?", (user_id,))
latest = row[0] if row and row[0] else None
# The daily rows live in `health_data`; `health_daily` is the name of
# the *writer* (health.upsert_health_daily), not of any table. Reading
# it raised inside the background thread, so an incremental sync died
# silently and left the status stuck on "syncing".
row = query_one(
"SELECT MAX(date) AS latest FROM health_data WHERE user_id = ?", (user_id,)
)
latest = row["latest"] if row and row.get("latest") else None
if latest is None:
days = DEFAULT_SYNC_DAYS # first sync → fall back to default window
else:
@@ -1038,12 +1044,21 @@ def sync_data(user_id, creds, days=None, client=None):
days_synced = 0
day_errors = []
rate_limited_mid_run = None
for i in range(days):
date_str = (today - datetime.timedelta(days=i)).isoformat()
try:
record = _extract_daily(client, date_str)
record.update(extras.daily_extras(client, date_str))
except Exception as e:
# A 429 inside the day loop used to be filed as one more skipped
# day, so a 730-day backfill kept hammering Garmin for another
# 700 days and drove the throttle deeper. Stand down at the first
# one and keep whatever was already stored.
if isinstance(e, RateLimited) or _is_rate_limited(e):
_note_rate_limit(user_id)
rate_limited_mid_run = describe(e)
break
day_errors.append(f"{date_str}: {describe(e)}")
continue
# A day Garmin has no data for comes back all-None; storing it would
@@ -1060,15 +1075,34 @@ def sync_data(user_id, creds, days=None, client=None):
except Exception as e: # noqa: BLE001
day_errors.append(f"{date_str} series: {describe(e)}")
# Reported every few days rather than every day: the write is cheap
# but not free, and the UI polls on a 2s cadence anyway.
if (i + 1) % 5 == 0 or i + 1 == days:
# A long backfill reports every fifth day the write is cheap but not
# free. A short one reports every day: at 7 days a "every 5th" cadence
# meant the bar sat at 0 for most of the run and then vanished.
if days <= 30 or (i + 1) % 5 == 0 or i + 1 == days:
_set_sync_status(
user_id, "syncing", now,
records_synced=days_synced, progress_current=i + 1,
progress_total=days, stage=f"每日数据 {date_str}",
)
if rate_limited_mid_run:
until, msg = _rate_limit_block(user_id)
message = msg or "Garmin 限制了请求频率,已自动退避。"
_set_sync_status(
user_id, "rate_limited", now, records_synced=days_synced,
progress_current=days_synced, progress_total=days, stage=None,
last_error=message,
)
return {
"status": "rate_limited",
"recordsSynced": days_synced,
"message": f"已同步 {days_synced} 天后被 Garmin 限流:{message}",
"retryAfterSeconds": (
int((until - datetime.datetime.utcnow()).total_seconds()) if until else None
),
"lastSyncTime": now,
}
activities_synced = 0
_set_sync_status(user_id, "syncing", now, records_synced=days_synced,
progress_current=days, progress_total=days,

View File

@@ -130,7 +130,8 @@ def sync_all_accounts(days=None, respect_schedule=False):
When `days` is not provided, each account's saved `history_days` from
`user_settings` is used (the user's 历史范围 picker), falling back to
`SYNC_DAYS`.
`SYNC_DAYS`. The scheduled loop always passes `days` explicitly — 历史范围
is the window for the manual 全量同步, not for a half-hourly tick.
"""
rows = query_all("SELECT user_id FROM garmin_tokens")
results = []
@@ -150,8 +151,14 @@ def sync_all_accounts(days=None, respect_schedule=False):
# Resolve the sync window: prefer the user's saved history_days,
# then the caller override, then the global default.
if days is None:
s = query_one("SELECT history_days FROM user_settings WHERE user_id = ?", (uid,))
user_days = s[0] if s and s[0] is not None else None
# query_one returns a dict, so `s[0]` raised KeyError — caught
# by the per-account handler below, which meant every single
# scheduled tick failed for every account and nothing was ever
# synced automatically.
s = query_one(
"SELECT history_days FROM user_settings WHERE user_id = ?", (uid,)
)
user_days = s.get("history_days") if s else None
if user_days == 0:
user_days = 730 # 全部历史 → 最大范围
d = user_days if user_days is not None else SYNC_DAYS
@@ -180,7 +187,12 @@ def _loop():
try:
if claim(interval=TICK_SECONDS):
try:
sync_all_accounts(respect_schedule=True)
# Explicitly the recent window, never the user's 历史范围:
# that setting describes the manual 全量同步. Letting a
# half-hourly tick re-pull "全部历史" meant 730 days x ~7
# Garmin calls every 30 minutes, which is precisely what
# kept the account in a 429 loop.
sync_all_accounts(days=SYNC_DAYS, respect_schedule=True)
finally:
release()
except Exception as e: # noqa: BLE001 - the loop must outlive any single failure