Skip to content

Commit

Permalink
move saving all media items linked to a source when the source is sav…
Browse files Browse the repository at this point in the history
…ed to recalculate media skip status to background task, resolves #94
  • Loading branch information
meeb committed Jul 14, 2024
1 parent 94dbe8b commit b6cec8a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 7 additions & 8 deletions tubesync/sync/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ def source_post_save(sender, instance, created, **kwargs):
verbose_name=verbose_name.format(instance.name),
remove_existing_tasks=True
)
# Trigger the post_save signal for each media item linked to this source as various
# flags may need to be recalculated
for media in Media.objects.filter(source=instance):
media.save()
verbose_name = _('Checking all media for source "{}"')
save_all_media_for_source(
str(instance.pk),
priority=0,
verbose_name=verbose_name.format(instance.name),
remove_existing_tasks=True
)


@receiver(pre_delete, sender=Source)
Expand All @@ -82,7 +85,6 @@ def source_pre_delete(sender, instance, **kwargs):
media.delete()



@receiver(post_delete, sender=Source)
def source_post_delete(sender, instance, **kwargs):
# Triggered after a source is deleted
Expand Down Expand Up @@ -173,8 +175,6 @@ def media_post_save(sender, instance, created, **kwargs):
)




@receiver(pre_delete, sender=Media)
def media_pre_delete(sender, instance, **kwargs):
# Triggered before media is deleted, delete any scheduled tasks
Expand All @@ -195,7 +195,6 @@ def media_pre_delete(sender, instance, **kwargs):
delete_file(file)



@receiver(post_delete, sender=Media)
def media_post_delete(sender, instance, **kwargs):
# Schedule a task to update media servers
Expand Down
22 changes: 22 additions & 0 deletions tubesync/sync/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def map_task_to_instance(task):
'sync.tasks.check_source_directory_exists': Source,
'sync.tasks.download_media_thumbnail': Media,
'sync.tasks.download_media': Media,
'sync.tasks.save_all_media_for_source': Source,
}
MODEL_URL_MAP = {
Source: 'sync:source',
Expand Down Expand Up @@ -464,3 +465,24 @@ def rescan_media_server(mediaserver_id):
# Request an rescan / update
log.info(f'Updating media server: {mediaserver}')
mediaserver.update()


@background(schedule=0)
def save_all_media_for_source(source_id):
'''
Iterates all media items linked to a source and saves them to
trigger the post_save signal for every media item. Used when a
source has its parameters changed and all media needs to be
checked to see if its download status has changed.
'''
try:
source = Source.objects.get(pk=source_id)
except Source.DoesNotExist:
# Task triggered but the source no longer exists, do nothing
log.error(f'Task save_all_media_for_source(pk={source_id}) called but no '
f'source exists with ID: {source_id}')
return
# Trigger the post_save signal for each media item linked to this source as various
# flags may need to be recalculated
for media in Media.objects.filter(source=source):
media.save()

1 comment on commit b6cec8a

@timwhite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly the task I was going to split out! Nicely done. 👍

Please sign in to comment.