a5101494b6
Today's platform-cooldown commit (61ce1ce) only filtered the scan tick
— manual /api/sources/<id>/check still bypassed it. Operator-flagged
2026-05-30: clicked "Retry failed" on a Patreon failure pile and saw
every one go 'queued' without realising the cooldown wasn't in the
loop. Bulk retry with N sources on a cooled-down platform bowls right
back into the rate limit the cooldown is trying to prevent.
**Backend (`/api/sources/<id>/check`):**
- Reads optional `?force=true` query flag.
- Without force: queries `active_platform_cooldowns` (renamed from the
private `_platforms_in_cooldown` since it's now a cross-module API).
If the source's platform is in cooldown, returns **202** with
`{status: 'deferred', platform, cooldown_until}` — no event created,
no dispatch.
- With force: cooldown skipped entirely.
- In-flight guard always applies (no point creating duplicate pendings).
**Frontend (`sourcesStore.checkNow(id, {force=false})`):** new optional
`force` flag → adds `?force=true` to the URL.
**Frontend (`DownloadsTab`):**
- `onRetrySource` (single-source RETRY click): passes `force: true` →
explicit operator override, useful for rapid auth-fix testing.
- `onRetryAll` (RETRY ALL + MaintenanceMenu "Retry failed"): no force →
cooldown respected. Tallies `deferred` alongside `queued` /
`already_running`; toast reads e.g. *"5 queued, 12 deferred
(cooldown), 3 already running"*. That count is the operator's
diagnostic answer for "is rate-limit the cause of most failures?"
(12-of-20 deferred → yes; 0 deferred → no).
**Auto-resume:** no new sweep needed. Deferred sources still have stale
`last_checked_at`, so the next scan tick after the cooldown AppSetting
expires picks them up via `select_due_sources` (which already filters
on `active_platform_cooldowns`).
Tests: two new — deferred-on-cooldown returns 202 with the right body
and no dispatch; force=true overrides the cooldown and creates the
event normally.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
import pytest
|
|
|
|
from backend.app.models import Artist, DownloadEvent, Source
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.fixture
|
|
async def seed(db):
|
|
artist = Artist(name="Alice", slug="alice")
|
|
db.add(artist)
|
|
await db.flush()
|
|
source = Source(
|
|
artist_id=artist.id, platform="patreon",
|
|
url="https://patreon.com/alice", enabled=True,
|
|
config_overrides={},
|
|
)
|
|
db.add(source)
|
|
await db.commit()
|
|
return source
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_202_creates_pending_event(client, seed, monkeypatch):
|
|
monkeypatch.setattr(
|
|
"backend.app.tasks.download.download_source.delay",
|
|
lambda *a, **k: None,
|
|
)
|
|
resp = await client.post(f"/api/sources/{seed.id}/check")
|
|
assert resp.status_code == 202
|
|
body = await resp.get_json()
|
|
assert body["status"] == "pending"
|
|
assert isinstance(body["download_event_id"], int)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_404_for_unknown_source(client):
|
|
resp = await client.post("/api/sources/99999/check")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_400_for_disabled_source(client, db, seed):
|
|
seed.enabled = False
|
|
await db.commit()
|
|
resp = await client.post(f"/api/sources/{seed.id}/check")
|
|
assert resp.status_code == 400
|
|
body = await resp.get_json()
|
|
assert body["error"] == "source_disabled"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_409_when_already_running(client, db, seed, monkeypatch):
|
|
monkeypatch.setattr(
|
|
"backend.app.tasks.download.download_source.delay",
|
|
lambda *a, **k: None,
|
|
)
|
|
existing = DownloadEvent(source_id=seed.id, status="running")
|
|
db.add(existing)
|
|
await db.commit()
|
|
await db.refresh(existing)
|
|
resp = await client.post(f"/api/sources/{seed.id}/check")
|
|
assert resp.status_code == 409
|
|
body = await resp.get_json()
|
|
assert body["download_event_id"] == existing.id
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_defers_when_platform_in_cooldown(client, db, seed, monkeypatch):
|
|
"""Bulk retries land here without ?force — when the source's platform
|
|
has an active cooldown, return 202 with status='deferred' instead of
|
|
creating an event. Lets the bulk path tally deferred-vs-queued in
|
|
the toast so the operator can see rate-limit-induced failures at a
|
|
glance."""
|
|
from backend.app.services.scheduler_service import set_platform_cooldown
|
|
|
|
delays: list = []
|
|
monkeypatch.setattr(
|
|
"backend.app.tasks.download.download_source.delay",
|
|
lambda *a, **k: delays.append(a),
|
|
)
|
|
|
|
await set_platform_cooldown(db, seed.platform, seconds=900)
|
|
await db.commit()
|
|
|
|
resp = await client.post(f"/api/sources/{seed.id}/check")
|
|
assert resp.status_code == 202
|
|
body = await resp.get_json()
|
|
assert body["status"] == "deferred"
|
|
assert body["platform"] == seed.platform
|
|
assert body["cooldown_until"]
|
|
assert delays == [] # no dispatch when deferred
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_force_overrides_cooldown(client, db, seed, monkeypatch):
|
|
"""Single-source RETRY click sends ?force=true — explicit operator
|
|
override, used for rapid auth-fix testing without waiting on the
|
|
cooldown."""
|
|
from backend.app.services.scheduler_service import set_platform_cooldown
|
|
|
|
delays: list = []
|
|
monkeypatch.setattr(
|
|
"backend.app.tasks.download.download_source.delay",
|
|
lambda *a, **k: delays.append(a),
|
|
)
|
|
|
|
await set_platform_cooldown(db, seed.platform, seconds=900)
|
|
await db.commit()
|
|
|
|
resp = await client.post(f"/api/sources/{seed.id}/check?force=true")
|
|
assert resp.status_code == 202
|
|
body = await resp.get_json()
|
|
assert body["status"] == "pending"
|
|
assert isinstance(body["download_event_id"], int)
|
|
assert len(delays) == 1
|
|
assert delays[0] == (seed.id,)
|