This commit is contained in:
Petitminion 2025-03-25 17:15:27 +01:00
parent bc0bd7f736
commit 612ab4888e
3 changed files with 47 additions and 4 deletions

View File

@ -18,13 +18,17 @@ from funkwhale_api.taskapp import celery
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class TooManyQueriesError(Exception):
pass
def check_existing_download_task(track): def check_existing_download_task(track):
if models.Upload.objects.filter( if models.Upload.objects.filter(
track=track, track=track,
import_status__in=["pending", "finished"], import_status__in=["pending", "finished"],
third_party_provider="archive-dl", third_party_provider="archive-dl",
).exists(): ).exists():
raise Exception( raise TooManyQueriesError(
"Upload for this track already exist or is pending. Stopping task." "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) time.sleep(2)
count += 1 count += 1
if count > 3: if count > 3:
raise Exception( raise TooManyQueriesError(
"Probably too many archivedl tasks are queue, stopping this task" "Probably too many archivedl tasks are queue, stopping this task"
) )
check_last_third_party_queries(track, count) 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.app.task(name="archivedl.archive_download")
@celery.require_instance(models.Track.objects.select_related(), "track") @celery.require_instance(models.Track.objects.select_related(), "track")
def archive_download(track, conf): def archive_download(track, conf):
check_existing_download_task(track) try:
check_last_third_party_queries(track, 0) 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) artist_name = utils.get_artist_credit_string(track)
query = f"mediatype:audio AND title:{track.title} AND creator:{artist_name}" query = f"mediatype:audio AND title:{track.title} AND creator:{artist_name}"
with requests.Session() as session: with requests.Session() as session:

View File

View File

@ -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
)