1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
Files
AdministrationItDepartmens/backend/app/tasks/cleanup.py
2025-01-05 05:43:45 +06:00

38 lines
1.4 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.

import os
from datetime import datetime, timedelta
from sqlalchemy.orm import Session
from app.database import SessionLocal
from app.models.chat import Message, ChatFile
def cleanup_old_messages():
"""Удаляет сообщения и файлы старше 1 месяца"""
db = SessionLocal()
try:
# Определяем дату, до которой нужно удалить сообщения
cutoff_date = datetime.utcnow() - timedelta(days=30)
# Получаем файлы, которые нужно удалить
files_to_delete = db.query(ChatFile)\
.join(Message)\
.filter(Message.created_at < cutoff_date)\
.all()
# Удаляем физические файлы
for file in files_to_delete:
try:
if os.path.exists(file.file_path):
os.remove(file.file_path)
except Exception as e:
print(f"Error deleting file {file.file_path}: {e}")
# Удаляем старые сообщения (каскадно удалятся и записи о файлах)
db.query(Message)\
.filter(Message.created_at < cutoff_date)\
.delete(synchronize_session=False)
db.commit()
except Exception as e:
print(f"Error during cleanup: {e}")
db.rollback()
finally:
db.close()