From 3e0a71f2efdf7b97e690d63db443b3efa7ddca7e Mon Sep 17 00:00:00 2001 From: meeb Date: Sat, 4 Sep 2021 15:00:23 +1000 Subject: [PATCH] add cli commands to list and delete sources, useful for deleting massive sources that currently time out as discussed in #148 --- .../sync/management/commands/delete-source.py | 51 +++++++++++++++++++ .../sync/management/commands/list-sources.py | 15 ++++++ 2 files changed, 66 insertions(+) create mode 100644 tubesync/sync/management/commands/delete-source.py create mode 100644 tubesync/sync/management/commands/list-sources.py diff --git a/tubesync/sync/management/commands/delete-source.py b/tubesync/sync/management/commands/delete-source.py new file mode 100644 index 0000000..104ec88 --- /dev/null +++ b/tubesync/sync/management/commands/delete-source.py @@ -0,0 +1,51 @@ +import os +import uuid +from django.utils.translation import gettext_lazy as _ +from django.core.management.base import BaseCommand, CommandError +from django.db.models import signals +from common.logger import log +from sync.models import Source, Media, MediaServer +from sync.signals import media_post_delete +from sync.tasks import rescan_media_server + + +class Command(BaseCommand): + + help = ('Deletes a source by UUID') + + def add_arguments(self, parser): + parser.add_argument('--source', action='store', required=True, help='Source UUID') + + def handle(self, *args, **options): + source_uuid_str = options.get('source', '') + try: + source_uuid = uuid.UUID(source_uuid_str) + except Exception as e: + raise CommandError(f'Failed to parse source UUID: {e}') + log.info(f'Deleting source with UUID: {source_uuid}') + # Fetch the source by UUID + try: + source = Source.objects.get(uuid=source_uuid) + except Source.DoesNotExist: + raise CommandError(f'Source does not exist with ' + f'UUID: {source_uuid}') + # Detach post-delete signal for Media so we don't spam media servers + signals.post_delete.disconnect(media_post_delete, sender=Media) + # Delete the source, triggering pre-delete signals for each media item + log.info(f'Found source with UUID "{source.uuid}" with name ' + f'"{source.name}" and deleting it, this may take some time!') + source.delete() + # Update any media servers + for mediaserver in MediaServer.objects.all(): + log.info(f'Scheduling media server updates') + verbose_name = _('Request media server rescan for "{}"') + rescan_media_server( + str(mediaserver.pk), + priority=0, + verbose_name=verbose_name.format(mediaserver), + remove_existing_tasks=True + ) + # Re-attach signals + signals.post_delete.connect(media_post_delete, sender=Media) + # All done + log.info('Done') diff --git a/tubesync/sync/management/commands/list-sources.py b/tubesync/sync/management/commands/list-sources.py new file mode 100644 index 0000000..4ee177a --- /dev/null +++ b/tubesync/sync/management/commands/list-sources.py @@ -0,0 +1,15 @@ +import os +from django.core.management.base import BaseCommand, CommandError +from common.logger import log +from sync.models import Source, Media, MediaServer + + +class Command(BaseCommand): + + help = ('Lists sources') + + def handle(self, *args, **options): + log.info('Listing sources...') + for source in Source.objects.all(): + log.info(f' - {source.uuid}: {source.name}') + log.info('Done')