diff --git a/tubesync/sync/models/media__tasks.py b/tubesync/sync/models/media__tasks.py index 06aa46b2..6f820c4c 100644 --- a/tubesync/sync/models/media__tasks.py +++ b/tubesync/sync/models/media__tasks.py @@ -103,12 +103,12 @@ def copy_thumbnail(self): if not self.source.copy_thumbnails: return if not self.thumb_file_exists: - from sync.tasks import delete_task_by_media, download_media_thumbnail + from sync.tasks import delete_task_by_media, download_media_image args = ( str(self.pk), self.thumbnail, ) if not args[1]: return delete_task_by_media('sync.tasks.download_media_thumbnail', args) - if download_media_thumbnail.now(*args): + if download_media_image.call_local(*args): self.refresh_from_db() if not self.thumb_file_exists: return diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 97a63515..c893c4eb 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -497,17 +497,15 @@ def index_source_task(source_id): log.info(f'Indexed new media: {source} / {media}') log.info(f'Scheduling tasks to download thumbnail for: {media.key}') thumbnail_fmt = 'https://i.ytimg.com/vi/{}/{}default.jpg' - vn_fmt = _('Downloading {} thumbnail for: "{}": {}') - for num, prefix in enumerate(('hq', 'sd', 'maxres',)): + for num, prefix in enumerate(reversed(('hq', 'sd', 'maxres',))): thumbnail_url = thumbnail_fmt.format( media.key, prefix, ) - download_media_thumbnail( - str(media.pk), - thumbnail_url, - schedule=dict(run_at=10+(300*num)), - verbose_name=vn_fmt.format(prefix, media.key, media.name), + download_media_image.schedule( + (str(media.pk), thumbnail_url,), + priority=10+(5*num), + delay=65-(30*num), ) log.info(f'Scheduling task to download metadata for: {media.url}') verbose_name = _('Downloading metadata for: "{}": {}') @@ -702,8 +700,8 @@ def download_media_metadata(media_id): f'{source} / {media}: {media_id}') -@background(schedule=dict(priority=10, run_at=10), queue=Val(TaskQueue.FS), remove_existing_tasks=True) -def download_media_thumbnail(media_id, url): +@dynamic_retry(db_task, delay=10, priority=90, retries=15, queue=Val(TaskQueue.NET)) +def download_media_image(media_id, url): ''' Downloads an image from a URL and save it as a local thumbnail attached to a Media instance. @@ -712,7 +710,7 @@ def download_media_thumbnail(media_id, url): media = Media.objects.get(pk=media_id) except Media.DoesNotExist as e: # Task triggered but the media no longer exists, do nothing - raise InvalidTaskError(_('no such media')) from e + raise CancelExecution(_('no such media'), retry=False) from e if media.skip or media.manual_skip: # Media was toggled to be skipped after the task was scheduled log.warn(f'Download task triggered for media: {media} (UUID: {media.pk}) but ' @@ -728,7 +726,7 @@ def download_media_thumbnail(media_id, url): raise raise NoThumbnailException(re.response.reason) from re except NoThumbnailException as e: - raise InvalidTaskError(str(e.__cause__)) from e + raise CancelExecution(str(e.__cause__), retry=False) from e if (i.width > width) and (i.height > height): log.info(f'Resizing {i.width}x{i.height} thumbnail to ' f'{width}x{height}: {url}') @@ -759,6 +757,12 @@ def download_media_thumbnail(media_id, url): copyfile(media.thumb.path, media.thumbpath) return True +@background(schedule=dict(priority=10, run_at=10), queue=Val(TaskQueue.FS), remove_existing_tasks=True) +def download_media_thumbnail(media_id, url): + try: + return download_media_image.call_local(media_id, url) + except CancelExecution as e: + raise InvalidTaskError(str(e)) from e @background(schedule=dict(priority=30, run_at=60), queue=Val(TaskQueue.NET), remove_existing_tasks=True) def download_media(media_id, override=False):