diff --git a/server/jobs.py b/server/jobs.py index 617ced8..918cee6 100644 --- a/server/jobs.py +++ b/server/jobs.py @@ -48,7 +48,8 @@ def get_job(job_id: str) -> Optional["Job"]: return JOBS.get(job_id) -def run_job(job_id: str, fn: Callable[[], dict], done_message: str, +def run_job(job_id: str, fn: Callable[[], dict], + done_message: "str | Callable[[dict], str]", fail_message: str = "Something went wrong while fetching.") -> None: def _task(): job = JOBS.get(job_id) @@ -57,7 +58,8 @@ def run_job(job_id: str, fn: Callable[[], dict], done_message: str, _touch(job, status="running") try: result = fn() - _touch(job, status="done", result=result, message=done_message) + msg = done_message(result) if callable(done_message) else done_message + _touch(job, status="done", result=result, message=msg) except Exception as e: # noqa: BLE001 — record any failure on the job _touch(job, status="failed", error=f"{type(e).__name__}: {e}", message=fail_message) diff --git a/server/mf.py b/server/mf.py index 3db34ca..c3eefef 100644 --- a/server/mf.py +++ b/server/mf.py @@ -24,6 +24,11 @@ act_youtube = _mod.act_youtube act_lidarr_album = _mod.act_lidarr_album act_lidarr_artist = _mod.act_lidarr_artist QUALITY_CHOICES = _mod.QUALITY_CHOICES +is_url = _mod.is_url +is_playlist_url = _mod.is_playlist_url +download_playlist = _mod.download_playlist +download_single = _mod.download_single __all__ = ["Hit", "build_combined_hits", "pick", "act_youtube", - "act_lidarr_album", "act_lidarr_artist", "QUALITY_CHOICES"] + "act_lidarr_album", "act_lidarr_artist", "QUALITY_CHOICES", + "is_url", "is_playlist_url", "download_playlist", "download_single"] diff --git a/tests/test_jobs.py b/tests/test_jobs.py index 74a7914..d61f80f 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -51,5 +51,13 @@ def test_eviction_keeps_within_cap(): jobs.JOBS.clear() +def test_run_job_callable_done_message(): + job = jobs.create_job(hit={}, message="m") + jobs.run_job(job.id, lambda: {"ok": 2, "total": 3}, + done_message=lambda res: f"{res['ok']}/{res['total']} done") + j = _wait(job.id, "done") + assert j.message == "2/3 done" + + def teardown_module(): jobs.JOBS.clear() diff --git a/tests/test_mf_url_exports.py b/tests/test_mf_url_exports.py new file mode 100644 index 0000000..6973ea0 --- /dev/null +++ b/tests/test_mf_url_exports.py @@ -0,0 +1,8 @@ +import server.mf as smf + + +def test_url_helpers_reexported(): + assert callable(smf.is_url) + assert callable(smf.is_playlist_url) + assert callable(smf.download_playlist) + assert callable(smf.download_single)