diff --git a/api/funkwhale_api/contrib/archivedl/tasks.py b/api/funkwhale_api/contrib/archivedl/tasks.py index 0e8c051a7..778328a7b 100644 --- a/api/funkwhale_api/contrib/archivedl/tasks.py +++ b/api/funkwhale_api/contrib/archivedl/tasks.py @@ -18,13 +18,17 @@ from funkwhale_api.taskapp import celery logger = logging.getLogger(__name__) +class TooManyQueriesError(Exception): + pass + + def check_existing_download_task(track): if models.Upload.objects.filter( track=track, import_status__in=["pending", "finished"], third_party_provider="archive-dl", ).exists(): - raise Exception( + raise TooManyQueriesError( "Upload for this track already exist or is pending. Stopping task." ) @@ -43,7 +47,7 @@ def check_last_third_party_queries(track, count): time.sleep(2) count += 1 if count > 3: - raise Exception( + raise TooManyQueriesError( "Probably too many archivedl tasks are queue, stopping this task" ) check_last_third_party_queries(track, count) @@ -78,8 +82,12 @@ def create_upload(url, track, files_data): @celery.app.task(name="archivedl.archive_download") @celery.require_instance(models.Track.objects.select_related(), "track") def archive_download(track, conf): - check_existing_download_task(track) - check_last_third_party_queries(track, 0) + try: + check_existing_download_task(track) + check_last_third_party_queries(track, 0) + except TooManyQueriesError as e: + logger.error(e) + return artist_name = utils.get_artist_credit_string(track) query = f"mediatype:audio AND title:{track.title} AND creator:{artist_name}" with requests.Session() as session: diff --git a/api/tests/contrib/archivedl/__init__.py b/api/tests/contrib/archivedl/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api/tests/contrib/archivedl/test_archivedl.py b/api/tests/contrib/archivedl/test_archivedl.py new file mode 100644 index 000000000..be3a93866 --- /dev/null +++ b/api/tests/contrib/archivedl/test_archivedl.py @@ -0,0 +1,35 @@ +import logging + +import pytest + +from funkwhale_api.contrib.archivedl import tasks + + +def test_check_existing_download_task(factories, caplog, mocker): + upload = factories["music.Upload"]( + third_party_provider="archive-dl", import_status="pending" + ) + mocker.patch("funkwhale_api.contrib.archivedl.tasks.fetch_json", return_value={}) + with pytest.raises( + tasks.TooManyQueriesError, + match=r".*Upload for this track already exist or is pending. Stopping task.*", + ): + tasks.archive_download(track_id=upload.track.id, conf={}) + + +def test_check_last_third_party_queries(factories, caplog, mocker): + logger = logging.getLogger("funkwhale_api.contrib.archivedl") + caplog.set_level(logging.INFO) + logger.addHandler(caplog.handler) + factories["music.Upload"].create_batch( + size=10, third_party_provider="archive-dl", import_status="pending" + ) + track = factories["music.Track"]() + mocker.patch("funkwhale_api.contrib.archivedl.tasks.fetch_json", return_value={}) + + with pytest.raises(KeyError): + tasks.archive_download(track_id=track.id, conf={}) + assert ( + "Last archive.org query was too recent. Trying to wait 2 seconds..." + in caplog.text + )