diff --git a/app/main.py b/app/main.py index 8d3e40bb..9ee4c384 100644 --- a/app/main.py +++ b/app/main.py @@ -83,7 +83,20 @@ def app_version() -> str: return "0.0.0-dev" +def _sweep_disabled() -> bool: + """The desktop app is a personal, user-curated library (folders + Trash), + with its track list persisted permanently in ~/Documents/StemDeck. The 24h + job TTL sweep -- a sensible disk-hygiene default for the shared server/Docker + deployment -- would wrongly purge stems the user kept, leaving orphaned + library entries that ask to "re-upload to restore". So skip the sweep under + the desktop shell (STEMDECK_DESKTOP=1); the user manages disk via Trash.""" + return os.environ.get("STEMDECK_DESKTOP") == "1" + + async def _sweep_loop() -> None: + if _sweep_disabled(): + _log.info("desktop mode: job TTL sweep disabled (library is user-managed)") + return while True: try: await asyncio.to_thread(sweep_old_jobs, JOBS_DIR) diff --git a/tests/test_sweep.py b/tests/test_sweep.py index 9d727d6d..944f9735 100644 --- a/tests/test_sweep.py +++ b/tests/test_sweep.py @@ -55,6 +55,29 @@ def test_sweeps_terminal_old_job(tmp_path: Path): assert job.id not in _jobs +def test_sweep_disabled_under_desktop(monkeypatch): + """The desktop shell (STEMDECK_DESKTOP=1) opts out of the TTL sweep so a + user's curated library isn't purged; the server/Docker default keeps it.""" + from app.main import _sweep_disabled + + monkeypatch.setenv("STEMDECK_DESKTOP", "1") + assert _sweep_disabled() is True + monkeypatch.delenv("STEMDECK_DESKTOP", raising=False) + assert _sweep_disabled() is False + + +@pytest.mark.asyncio +async def test_sweep_loop_returns_immediately_under_desktop(monkeypatch): + """In desktop mode the loop returns at once instead of entering the hourly + cycle (wait_for would time out if it looped).""" + import asyncio + + from app.main import _sweep_loop + + monkeypatch.setenv("STEMDECK_DESKTOP", "1") + await asyncio.wait_for(_sweep_loop(), timeout=2) + + def test_keeps_recent_terminal_job(tmp_path: Path): d = _mkdir(tmp_path, "abcdefabcded") job = Job(id="abcdefabcded")