55 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Celery задачи для модуля communications
"""
from celery import shared_task
from django.utils import timezone
from datetime import timedelta
import logging
logger = logging.getLogger(__name__)
@shared_task
def cleanup_orphaned_attachments():
"""
Очистка временных вложений без привязки к сообщению.
Удаляет MessageAttachment с message=null старше 1 часа.
"""
from communications.models import MessageAttachment
cutoff_time = timezone.now() - timedelta(hours=1)
orphaned = MessageAttachment.objects.filter(
message__isnull=True, uploaded_at__lt=cutoff_time
)
count = orphaned.count()
if count > 0:
logger.info(
f"[cleanup_orphaned_attachments] Found {count} orphaned attachments"
)
# Удаляем файлы и записи
for attachment in orphaned:
try:
if attachment.file:
attachment.file.delete()
attachment.delete()
except Exception as e:
logger.error(
f"[cleanup_orphaned_attachments] Error deleting attachment {
attachment.id
}: {e}"
)
logger.info(
(
"[cleanup_orphaned_attachments] Cleaned up "
f"{count} orphaned attachments"
)
)
return count