in cleanup_old_media, filter in database rather than in Python

This commit is contained in:
Alexey Kryuchkov 2023-05-27 00:59:28 +03:00
parent 0c5a9c53f8
commit 612f78e7eb
2 changed files with 39 additions and 11 deletions

View File

@ -132,13 +132,11 @@ def cleanup_completed_tasks():
def cleanup_old_media(): def cleanup_old_media():
for media in Media.objects.filter(download_date__isnull=False): for source in Source.objects.filter(delete_old_media=True, days_to_keep__gt=0):
if media.source.delete_old_media and media.source.days_to_keep > 0: delta = timezone.now() - timedelta(days=source.days_to_keep)
delta = timezone.now() - timedelta(days=media.source.days_to_keep) for media in source.media_source.filter(downloaded=True, download_date__lt=delta):
if media.downloaded and media.download_date < delta: log.info(f'Deleting expired media: {source} / {media} '
# Media was downloaded after the cutoff date, delete it f'(now older than {source.days_to_keep} days / '
log.info(f'Deleting expired media: {media.source} / {media} '
f'(now older than {media.source.days_to_keep} days / '
f'download_date before {delta})') f'download_date before {delta})')
# .delete() also triggers a pre_delete signal that removes the files # .delete() also triggers a pre_delete signal that removes the files
media.delete() media.delete()

View File

@ -6,7 +6,7 @@
import logging import logging
from datetime import datetime from datetime import datetime, timedelta
from urllib.parse import urlsplit from urllib.parse import urlsplit
from xml.etree import ElementTree from xml.etree import ElementTree
from django.conf import settings from django.conf import settings
@ -14,6 +14,7 @@ from django.test import TestCase, Client
from django.utils import timezone from django.utils import timezone
from background_task.models import Task from background_task.models import Task
from .models import Source, Media from .models import Source, Media
from .tasks import cleanup_old_media
class FrontEndTestCase(TestCase): class FrontEndTestCase(TestCase):
@ -1397,3 +1398,32 @@ class FormatMatchingTestCase(TestCase):
match_type, format_code = self.media.get_best_video_format() match_type, format_code = self.media.get_best_video_format()
self.assertEqual(format_code, expected_format_code) self.assertEqual(format_code, expected_format_code)
self.assertEqual(match_type, expeceted_match_type) self.assertEqual(match_type, expeceted_match_type)
class TasksTestCase(TestCase):
def setUp(self):
# Disable general logging for test case
logging.disable(logging.CRITICAL)
def test_delete_old_media(self):
src1 = Source.objects.create(key='aaa', name='aaa', directory='/tmp/a', delete_old_media=False, days_to_keep=14)
src2 = Source.objects.create(key='bbb', name='bbb', directory='/tmp/b', delete_old_media=True, days_to_keep=14)
now = timezone.now()
m11 = Media.objects.create(source=src1, downloaded=True, key='a11', download_date=now - timedelta(days=5))
m12 = Media.objects.create(source=src1, downloaded=True, key='a12', download_date=now - timedelta(days=25))
m13 = Media.objects.create(source=src1, downloaded=False, key='a13')
m21 = Media.objects.create(source=src2, downloaded=True, key='a21', download_date=now - timedelta(days=5))
m22 = Media.objects.create(source=src2, downloaded=True, key='a22', download_date=now - timedelta(days=25))
m23 = Media.objects.create(source=src2, downloaded=False, key='a23')
self.assertEquals(src1.media_source.all().count(), 3)
self.assertEquals(src2.media_source.all().count(), 3)
cleanup_old_media()
self.assertEquals(src1.media_source.all().count(), 3)
self.assertEquals(src2.media_source.all().count(), 2)
self.assertEquals(Media.objects.filter(pk=m22.pk).exists(), False)