tubesync/app/sync/signals.py

102 lines
3.9 KiB
Python
Raw Normal View History

2020-12-06 01:22:16 +00:00
from django.conf import settings
2020-12-07 10:26:46 +00:00
from django.db.models.signals import pre_save, post_save, pre_delete, post_delete
2020-12-06 01:22:16 +00:00
from django.dispatch import receiver
2020-12-07 10:26:46 +00:00
from django.utils.translation import gettext_lazy as _
from background_task.signals import task_failed
from background_task.models import Task
from common.logger import log
2020-12-06 01:22:16 +00:00
from .models import Source, Media
2020-12-07 10:26:46 +00:00
from .tasks import (delete_index_source_task, index_source_task,
download_media_thumbnail, map_task_to_instance)
2020-12-06 02:48:10 +00:00
from .utils import delete_file
2020-12-06 01:22:16 +00:00
2020-12-07 10:26:46 +00:00
@receiver(pre_save, sender=Source)
def source_pre_save(sender, instance, **kwargs):
# Triggered before a source is saved, if the schedule has been updated recreate
# its indexing task
try:
existing_source = Source.objects.get(pk=instance.pk)
except Source.DoesNotExist:
# Probably not possible?
return
if existing_source.index_schedule != instance.index_schedule:
# Indexing schedule has changed, recreate the indexing task
delete_index_source_task(str(instance.pk))
verbose_name = _('Index media from source "{}"')
index_source_task(
str(instance.pk),
repeat=instance.index_schedule,
queue=str(instance.pk),
2020-12-07 10:26:46 +00:00
verbose_name=verbose_name.format(instance.name)
)
2020-12-06 01:22:16 +00:00
@receiver(post_save, sender=Source)
def source_post_save(sender, instance, created, **kwargs):
2020-12-07 10:26:46 +00:00
# Triggered after a source is saved
if created:
# Create a new indexing task for newly created sources
delete_index_source_task(str(instance.pk))
log.info(f'Scheduling media indexing for source: {instance.name}')
verbose_name = _('Index media from source "{}"')
index_source_task(
str(instance.pk),
repeat=instance.index_schedule,
queue=str(instance.pk),
2020-12-07 10:26:46 +00:00
verbose_name=verbose_name.format(instance.name)
)
2020-12-06 01:22:16 +00:00
2020-12-06 02:48:10 +00:00
@receiver(pre_delete, sender=Source)
def source_post_delete(sender, instance, **kwargs):
2020-12-07 10:26:46 +00:00
# Triggered before a source is deleted, delete all media objects to trigger
2020-12-06 02:48:10 +00:00
# the Media models post_delete signal
for media in Media.objects.filter(source=instance):
2020-12-07 10:26:46 +00:00
log.info(f'Deleting media for source: {instance.name} item: {media.name}')
2020-12-06 02:48:10 +00:00
media.delete()
2020-12-06 01:22:16 +00:00
@receiver(post_delete, sender=Source)
def source_post_delete(sender, instance, **kwargs):
2020-12-07 10:26:46 +00:00
# Triggered after a source is deleted
log.info(f'Deleting tasks for source: {instance.name}')
2020-12-06 01:22:16 +00:00
delete_index_source_task(str(instance.pk))
2020-12-07 10:26:46 +00:00
@receiver(task_failed, sender=Task)
def task_task_failed(sender, task_id, completed_task, **kwargs):
# Triggered after a source fails by reaching its max retry attempts
obj, url = map_task_to_instance(completed_task)
if isinstance(obj, Source):
log.error(f'Permanent failure for source: {obj} task: {completed_task}')
obj.has_failed = True
obj.save()
2020-12-06 01:22:16 +00:00
@receiver(post_save, sender=Media)
def media_post_save(sender, instance, created, **kwargs):
2020-12-07 10:26:46 +00:00
# Triggered after media is saved
2020-12-06 01:22:16 +00:00
if created:
# If the media is newly created fire a task off to download its thumbnail
metadata = instance.loaded_metadata
thumbnail_url = metadata.get('thumbnail', '')
if thumbnail_url:
2020-12-07 10:26:46 +00:00
log.info(f'Scheduling task to download thumbnail for: {instance.name} '
f'from: {thumbnail_url}')
verbose_name = _('Downloading media thumbnail for "{}"')
2020-12-07 10:26:46 +00:00
download_media_thumbnail(
str(instance.pk),
thumbnail_url,
queue=str(instance.source.pk),
2020-12-07 10:26:46 +00:00
verbose_name=verbose_name.format(instance.name)
)
2020-12-06 01:22:16 +00:00
@receiver(post_delete, sender=Media)
def media_post_delete(sender, instance, **kwargs):
2020-12-07 10:26:46 +00:00
# Triggered after media is deleted, delete media thumbnail
if instance.thumb:
2020-12-07 10:26:46 +00:00
log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}')
delete_file(instance.thumb.path)