Merge pull request #1049 from tcely/patch-3

Tweaks for recent changes
This commit is contained in:
meeb 2025-05-20 17:44:10 +10:00 committed by GitHub
commit c434eb2487
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 7 deletions

View File

@ -552,7 +552,7 @@ class Source(db.models.Model):
entries.maxlen // 2, entries.maxlen // 2,
entries.maxlen - len(entries), entries.maxlen - len(entries),
) )
entries.extend(reversed(streams[-1 * allowed_streams :])) entries.extend(reversed(streams[: allowed_streams]))
return entries return entries

View File

@ -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() window = timezone.timedelta(hours=3) + timezone.now()
tqs = Task.objects.filter( tqs = Task.objects.filter(
task_name=tn, task_name__in=task_name,
attempts__gt=0, attempts__gt=0,
locked_at__isnull=True, locked_at__isnull=True,
run_at__lte=window, run_at__lte=window,
last_error__contains='HTTPError 429: Too Many Requests', last_error__contains='HTTPError 429: Too Many Requests',
) )
task = get_first_task(tn, instance=media) for task in tasks:
update_task_status(task, 'paused (429)') update_task_status(task, 'paused (429)')
log.info(f'waiting for errors: 429 ({tqs.count()}): {model}')
time.sleep(10 * tqs.count()) time.sleep(10 * tqs.count())
update_task_status(task, None) for task in tasks:
update_task_status(task, None)
def cleanup_old_media(): 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.') log.info(f'Task for ID: {media_id} / {media} skipped, due to task being manually skipped.')
return return
source = media.source source = media.source
wait_for_errors(media) wait_for_errors(media, task_name='sync.tasks.download_media_metadata')
try: try:
metadata = media.index_metadata() metadata = media.index_metadata()
except YouTubeError as e: except YouTubeError as e:
@ -637,6 +651,7 @@ def download_media(media_id, override=False):
# should raise an exception to avoid this # should raise an exception to avoid this
return return
wait_for_errors(media, task_name='sync.tasks.download_media')
filepath = media.filepath filepath = media.filepath
container = format_str = None container = format_str = None
log.info(f'Downloading media: {media} (UUID: {media.pk}) to: "{filepath}"') log.info(f'Downloading media: {media} (UUID: {media.pk}) to: "{filepath}"')