From 869295b5321fe1c225a66a0004c104a337e8f483 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 13 Nov 2024 10:45:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AE=9A=E6=97=B6=E6=B8=85?= =?UTF-8?q?=E7=90=86=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E6=B2=A1=E5=85=B3?= =?UTF-8?q?=E8=81=94=E5=BA=94=E7=94=A8=E7=9A=84=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/job/__init__.py | 2 -- apps/common/job/clean_chat_job.py | 3 ++ apps/common/job/clean_orphaned_file_job.py | 40 ---------------------- apps/dataset/models/data_set.py | 8 +++++ 4 files changed, 11 insertions(+), 42 deletions(-) delete mode 100644 apps/common/job/clean_orphaned_file_job.py diff --git a/apps/common/job/__init__.py b/apps/common/job/__init__.py index 5d75a6c35..2f4ef2697 100644 --- a/apps/common/job/__init__.py +++ b/apps/common/job/__init__.py @@ -6,7 +6,6 @@ @date:2024/3/14 11:54 @desc: """ -from .clean_orphaned_file_job import * from .client_access_num_job import * from .clean_chat_job import * @@ -14,4 +13,3 @@ from .clean_chat_job import * def run(): client_access_num_job.run() clean_chat_job.run() - clean_orphaned_file_job.run() diff --git a/apps/common/job/clean_chat_job.py b/apps/common/job/clean_chat_job.py index 23ff2c85a..332beee18 100644 --- a/apps/common/job/clean_chat_job.py +++ b/apps/common/job/clean_chat_job.py @@ -10,6 +10,7 @@ from django_apscheduler.jobstores import DjangoJobStore from application.models import Application, Chat from django.db.models import Q from common.lock.impl.file_lock import FileLock +from dataset.models import File scheduler = BackgroundScheduler() scheduler.add_jobstore(DjangoJobStore(), "default") @@ -38,6 +39,8 @@ def clean_chat_log_job(): if count == 0: break deleted_count, _ = Chat.objects.filter(id__in=logs_to_delete).delete() + # 删除对应的文件 + File.objects.filter(~Q(meta__chat_id__in=logs_to_delete)).delete() if deleted_count < batch_size: break diff --git a/apps/common/job/clean_orphaned_file_job.py b/apps/common/job/clean_orphaned_file_job.py deleted file mode 100644 index 69185150b..000000000 --- a/apps/common/job/clean_orphaned_file_job.py +++ /dev/null @@ -1,40 +0,0 @@ -# coding=utf-8 - -import logging - -from apscheduler.schedulers.background import BackgroundScheduler -from django.db.models import Q -from django_apscheduler.jobstores import DjangoJobStore - -from application.models import Chat -from common.lock.impl.file_lock import FileLock -from dataset.models import File - -scheduler = BackgroundScheduler() -scheduler.add_jobstore(DjangoJobStore(), "default") -lock = FileLock() - - -def clean_debug_file(): - logging.getLogger("max_kb").info('开始清理没有关联会话的上传文件') - existing_chat_ids = set(Chat.objects.values_list('id', flat=True)) - # UUID to str - existing_chat_ids = [str(chat_id) for chat_id in existing_chat_ids] - print(existing_chat_ids) - # 查找引用的不存在的 chat_id 并删除相关记录 - deleted_count, _ = File.objects.filter(~Q(meta__chat_id__in=existing_chat_ids)).delete() - - logging.getLogger("max_kb").info(f'结束清理没有关联会话的上传文件: {deleted_count}') - - -def run(): - if lock.try_lock('clean_orphaned_file_job', 30 * 30): - try: - scheduler.start() - clean_orphaned_file = scheduler.get_job(job_id='clean_orphaned_file') - if clean_orphaned_file is not None: - clean_orphaned_file.remove() - scheduler.add_job(clean_debug_file, 'cron', hour='2', minute='0', second='0', - id='clean_orphaned_file') - finally: - lock.un_lock('clean_orphaned_file_job') diff --git a/apps/dataset/models/data_set.py b/apps/dataset/models/data_set.py index fb86c30e3..cd91b6d18 100644 --- a/apps/dataset/models/data_set.py +++ b/apps/dataset/models/data_set.py @@ -9,6 +9,8 @@ import uuid from django.db import models +from django.db.models.signals import pre_delete +from django.dispatch import receiver from common.db.sql_execute import select_one from common.mixins.app_model_mixin import AppModelMixin @@ -157,3 +159,9 @@ class File(AppModelMixin): def get_byte(self): result = select_one(f'SELECT lo_get({self.loid}) as "data"', []) return result['data'] + + + +@receiver(pre_delete, sender=File) +def on_delete_file(sender, instance, **kwargs): + select_one(f'SELECT lo_unlink({instance.loid})', [])