fix cleaning up tasks on media deletion
This commit is contained in:
parent
90a8918e40
commit
5ade6c1648
|
@ -379,6 +379,10 @@ class Media(models.Model):
|
|||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'title',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'title',
|
||||
},
|
||||
'thumbnail': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'thumbnail',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'thumbnail',
|
||||
},
|
||||
'description': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'description',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'description',
|
||||
|
@ -600,6 +604,11 @@ class Media(models.Model):
|
|||
field = self.get_metadata_field('title')
|
||||
return self.loaded_metadata.get(field, '').strip()
|
||||
|
||||
@property
|
||||
def thumbnail(self):
|
||||
field = self.get_metadata_field('thumbnail')
|
||||
return self.loaded_metadata.get(field, '').strip()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
title = self.title
|
||||
|
|
|
@ -85,8 +85,7 @@ def media_post_save(sender, instance, created, **kwargs):
|
|||
# Triggered after media is saved
|
||||
if created:
|
||||
# If the media is newly created start a task to download its thumbnail
|
||||
metadata = instance.loaded_metadata
|
||||
thumbnail_url = metadata.get('thumbnail', '')
|
||||
thumbnail_url = instance.thumbnail
|
||||
if thumbnail_url:
|
||||
log.info(f'Scheduling task to download thumbnail for: {instance.name} '
|
||||
f'from: {thumbnail_url}')
|
||||
|
@ -109,7 +108,7 @@ def media_post_save(sender, instance, created, **kwargs):
|
|||
instance.save()
|
||||
# If the media has not yet been downloaded schedule it to be downloaded
|
||||
if not instance.downloaded:
|
||||
delete_task_by_media('sync.tasks.download_media', instance.pk)
|
||||
delete_task_by_media('sync.tasks.download_media', (str(instance.pk),))
|
||||
verbose_name = _('Downloading media for "{}"')
|
||||
download_media(
|
||||
str(instance.pk),
|
||||
|
@ -122,7 +121,11 @@ def media_post_save(sender, instance, created, **kwargs):
|
|||
def media_pre_delete(sender, instance, **kwargs):
|
||||
# Triggered before media is deleted, delete any scheduled tasks
|
||||
log.info(f'Deleting tasks for media: {instance.name}')
|
||||
delete_task_by_media('sync.tasks.download_media_thumbnail', instance.pk)
|
||||
delete_task_by_media('sync.tasks.download_media', (str(instance.pk),))
|
||||
thumbnail_url = instance.thumbnail
|
||||
if thumbnail_url:
|
||||
delete_task_by_media('sync.tasks.download_media_thumbnail',
|
||||
(str(instance.pk), thumbnail_url))
|
||||
# Delete media thumbnail if it exists
|
||||
if instance.thumb:
|
||||
log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}')
|
||||
|
|
|
@ -74,7 +74,6 @@ def map_task_to_instance(task):
|
|||
instance = model.objects.get(pk=instance_uuid)
|
||||
return instance, url
|
||||
except model.DoesNotExist:
|
||||
print('!!!', model, instance_uuid)
|
||||
return None, None
|
||||
|
||||
|
||||
|
@ -108,8 +107,8 @@ def delete_task_by_source(task_name, source_id):
|
|||
return Task.objects.filter(task_name=task_name, queue=str(source_id)).delete()
|
||||
|
||||
|
||||
def delete_task_by_media(task_name, media_id):
|
||||
return Task.objects.drop_task(task_name, args=(str(media_id),))
|
||||
def delete_task_by_media(task_name, args):
|
||||
return Task.objects.drop_task(task_name, args=args)
|
||||
|
||||
|
||||
def cleanup_completed_tasks():
|
||||
|
|
|
@ -291,6 +291,9 @@ class FrontEndTestCase(TestCase):
|
|||
found_thumbnail_task1 = False
|
||||
found_thumbnail_task2 = False
|
||||
found_thumbnail_task3 = False
|
||||
found_download_task1 = False
|
||||
found_download_task2 = False
|
||||
found_download_task3 = False
|
||||
q = {'queue': str(test_source.pk),
|
||||
'task_name': 'sync.tasks.download_media_thumbnail'}
|
||||
for task in Task.objects.filter(**q):
|
||||
|
@ -300,9 +303,21 @@ class FrontEndTestCase(TestCase):
|
|||
found_thumbnail_task2 = True
|
||||
if test_media3_pk in task.task_params:
|
||||
found_thumbnail_task3 = True
|
||||
q = {'queue': str(test_source.pk),
|
||||
'task_name': 'sync.tasks.download_media'}
|
||||
for task in Task.objects.filter(**q):
|
||||
if test_media1_pk in task.task_params:
|
||||
found_download_task1 = True
|
||||
if test_media2_pk in task.task_params:
|
||||
found_download_task2 = True
|
||||
if test_media3_pk in task.task_params:
|
||||
found_download_task3 = True
|
||||
self.assertTrue(found_thumbnail_task1)
|
||||
self.assertTrue(found_thumbnail_task2)
|
||||
self.assertTrue(found_thumbnail_task3)
|
||||
self.assertTrue(found_download_task1)
|
||||
self.assertTrue(found_download_task2)
|
||||
self.assertTrue(found_download_task3)
|
||||
# Check the media is listed on the media overview page
|
||||
response = c.get('/media')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
@ -329,8 +344,12 @@ class FrontEndTestCase(TestCase):
|
|||
response = c.get(f'/media/{test_media3_pk}')
|
||||
self.assertEqual(response.status_code, 404)
|
||||
# Confirm any tasks have been deleted
|
||||
tasks = Task.objects.filter(**q)
|
||||
self.assertFalse(tasks)
|
||||
q = {'task_name': 'sync.tasks.download_media_thumbnail'}
|
||||
download_media_thumbnail_tasks = Task.objects.filter(**q)
|
||||
self.assertFalse(download_media_thumbnail_tasks)
|
||||
q = {'task_name': 'sync.tasks.download_media'}
|
||||
download_media_tasks = Task.objects.filter(**q)
|
||||
self.assertFalse(download_media_tasks)
|
||||
|
||||
def test_tasks(self):
|
||||
# Tasks overview page
|
||||
|
|
|
@ -53,7 +53,6 @@ def download_media(url, media_format, extension, output_file):
|
|||
'outtmpl': output_file,
|
||||
'quiet': True,
|
||||
})
|
||||
print(opts)
|
||||
with youtube_dl.YoutubeDL(opts) as y:
|
||||
try:
|
||||
return y.download([url])
|
||||
|
|
Loading…
Reference in New Issue