Harden YouTube sync: retries + pooled client, uploads-playlist fallback, JSON 404 for /api/*
Findings from a full audit of the live instance, each with tests:
1. Transient TLS failures to Google ("[SSL: UNEXPECTED_EOF_WHILE_READING]")
were silently dropping 2-4% of channels from every videos sync and once
failed a whole scheduled subscriptions sync. youtube_client now retries
transport errors and 5xx (3 attempts, 0.5s/2s backoff; 4xx never), and
sync_videos runs under youtube_session() so the ~240 calls of a run share
one pooled client instead of a fresh TLS handshake each. Verified live:
one EOF during the next sync was retried and the run lost no channels.
2. channels.list omits some channels entirely (three region-restricted TV
channels here), so they never got an uploads_playlist_id and never had a
videos sync. The id is derivable ("UC..." -> "UU..."); sync_subscriptions
falls back to it, also when channels.list fails. Verified live: all three
now have 200 videos each.
3. SPAStaticFiles served index.html with HTTP 200 for any unknown /api/*
path (a mistyped endpoint "succeeded" with HTML). /api/* now stays a JSON
404; client-side routes still fall back to the SPA.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
906a142796
commit
f97ef0d54d
6 changed files with 370 additions and 36 deletions
|
|
@ -567,3 +567,77 @@ def test_is_videos_sync_running_reflects_lock():
|
|||
finally:
|
||||
sync._videos_lock.release()
|
||||
assert sync.is_videos_sync_running() is False
|
||||
|
||||
|
||||
def test_sync_subscriptions_derives_uploads_playlist_when_channels_list_omits_channel(monkeypatch, db_session):
|
||||
"""channels.list omits some channels entirely (seen in production for
|
||||
region-restricted TV channels) -- without a fallback they never get a
|
||||
videos sync. The uploads playlist id is derivable from the channel id."""
|
||||
monkeypatch.setattr(sync.google_oauth, "get_credentials", lambda db: object())
|
||||
monkeypatch.setattr(
|
||||
sync.youtube_client,
|
||||
"fetch_subscriptions",
|
||||
lambda creds: [
|
||||
{"youtube_channel_id": "UCknown00000000000000000", "youtube_subscription_id": "s1", "title": "Known", "description": "", "thumbnail_url": None},
|
||||
{"youtube_channel_id": "UComitted0000000000000000", "youtube_subscription_id": "s2", "title": "Omitted", "description": "", "thumbnail_url": None},
|
||||
{"youtube_channel_id": "UCnouploads00000000000000", "youtube_subscription_id": "s3", "title": "NoUploads", "description": "", "thumbnail_url": None},
|
||||
],
|
||||
)
|
||||
# Only the first channel comes back from channels.list; the third comes
|
||||
# back without an uploads playlist at all.
|
||||
monkeypatch.setattr(
|
||||
sync.youtube_client,
|
||||
"fetch_uploads_playlists",
|
||||
lambda creds, ids: {
|
||||
"UCknown00000000000000000": {"uploads_playlist_id": "UUfromapi", "subscriber_count": 5},
|
||||
"UCnouploads00000000000000": {"uploads_playlist_id": None, "subscriber_count": None},
|
||||
},
|
||||
)
|
||||
|
||||
sync.sync_subscriptions(db_session)
|
||||
|
||||
by_id = {c.youtube_channel_id: c for c in db_session.query(Channel).all()}
|
||||
assert by_id["UCknown00000000000000000"].uploads_playlist_id == "UUfromapi" # API value wins
|
||||
assert by_id["UComitted0000000000000000"].uploads_playlist_id == "UUomitted0000000000000000"
|
||||
assert by_id["UCnouploads00000000000000"].uploads_playlist_id == "UUnouploads00000000000000"
|
||||
|
||||
|
||||
def test_sync_subscriptions_derives_uploads_playlist_even_if_channels_list_fails(monkeypatch, db_session):
|
||||
monkeypatch.setattr(sync.google_oauth, "get_credentials", lambda db: object())
|
||||
monkeypatch.setattr(
|
||||
sync.youtube_client,
|
||||
"fetch_subscriptions",
|
||||
lambda creds: [
|
||||
{"youtube_channel_id": "UCabc", "youtube_subscription_id": "s1", "title": "A", "description": "", "thumbnail_url": None},
|
||||
],
|
||||
)
|
||||
|
||||
def boom(creds, ids):
|
||||
raise RuntimeError("channels.list down")
|
||||
|
||||
monkeypatch.setattr(sync.youtube_client, "fetch_uploads_playlists", boom)
|
||||
|
||||
result = sync.sync_subscriptions(db_session)
|
||||
|
||||
assert result["status"] == "completed"
|
||||
assert db_session.query(Channel).one().uploads_playlist_id == "UUabc"
|
||||
|
||||
|
||||
def test_sync_videos_uses_one_http_client_for_the_whole_run(monkeypatch, db_session):
|
||||
for i in range(3):
|
||||
_seed_channel(db_session, youtube_channel_id=f"chan{i}", uploads_playlist_id=f"UUchan{i}")
|
||||
constructed = []
|
||||
|
||||
def make_client(timeout):
|
||||
client = _FakeClient([_page(["v"]), _page(["v"]), _page(["v"]), _FakeResponse({"items": []})])
|
||||
constructed.append(client)
|
||||
return client
|
||||
|
||||
monkeypatch.setattr(sync.google_oauth, "get_credentials", lambda db: _FakeCredentials())
|
||||
monkeypatch.setattr(sync.youtube_client.httpx, "Client", make_client)
|
||||
|
||||
sync.sync_videos(db_session)
|
||||
|
||||
# 3 playlistItems calls + 1 videos.list call, all through a single client.
|
||||
assert len(constructed) == 1
|
||||
assert len(constructed[0].requests) == 4
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue