diff --git a/tubesync/sync/models/source.py b/tubesync/sync/models/source.py index d85334f0..07ade020 100644 --- a/tubesync/sync/models/source.py +++ b/tubesync/sync/models/source.py @@ -552,7 +552,7 @@ class Source(db.models.Model): entries.maxlen // 2, entries.maxlen - len(entries), ) - entries.extend(reversed(streams[-1 * allowed_streams :])) + entries.extend(reversed(streams[: allowed_streams])) return entries diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 61cb52a0..466e41d3 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -233,19 +233,33 @@ def schedule_media_servers_update(): ) -def wait_for_errors(media, /, tn='sync.tasks.download_media_metadata'): +def wait_for_errors(model, /, *, task_name=None): + if task_name is None: + task_name=tuple(( + 'sync.tasks.download_media', + 'sync.tasks.download_media_metadata', + )) + elif isinstance(task_name, str): + task_name = tuple((task_name,)) + tasks = list() + for tn in task_name: + ft = get_first_task(tn, instance=model) + if ft: + tasks.append(ft) window = timezone.timedelta(hours=3) + timezone.now() tqs = Task.objects.filter( - task_name=tn, + task_name__in=task_name, attempts__gt=0, locked_at__isnull=True, run_at__lte=window, last_error__contains='HTTPError 429: Too Many Requests', ) - task = get_first_task(tn, instance=media) - update_task_status(task, 'paused (429)') + for task in tasks: + update_task_status(task, 'paused (429)') + log.info(f'waiting for errors: 429 ({tqs.count()}): {model}') time.sleep(10 * tqs.count()) - update_task_status(task, None) + for task in tasks: + update_task_status(task, None) def cleanup_old_media(): @@ -484,7 +498,7 @@ def download_media_metadata(media_id): log.info(f'Task for ID: {media_id} / {media} skipped, due to task being manually skipped.') return source = media.source - wait_for_errors(media) + wait_for_errors(media, task_name='sync.tasks.download_media_metadata') try: metadata = media.index_metadata() except YouTubeError as e: @@ -637,6 +651,7 @@ def download_media(media_id, override=False): # should raise an exception to avoid this return + wait_for_errors(media, task_name='sync.tasks.download_media') filepath = media.filepath container = format_str = None log.info(f'Downloading media: {media} (UUID: {media.pk}) to: "{filepath}"')