Skip to content

Commit 869295b

Browse files
committed
refactor: 定时清理数据库中没关联应用的图片
1 parent 2ed8a9a commit 869295b

File tree

4 files changed

+11
-42
lines changed

4 files changed

+11
-42
lines changed

apps/common/job/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
@date:2024/3/14 11:54
77
@desc:
88
"""
9-
from .clean_orphaned_file_job import *
109
from .client_access_num_job import *
1110
from .clean_chat_job import *
1211

1312

1413
def run():
1514
client_access_num_job.run()
1615
clean_chat_job.run()
17-
clean_orphaned_file_job.run()

apps/common/job/clean_chat_job.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from application.models import Application, Chat
1111
from django.db.models import Q
1212
from common.lock.impl.file_lock import FileLock
13+
from dataset.models import File
1314

1415
scheduler = BackgroundScheduler()
1516
scheduler.add_jobstore(DjangoJobStore(), "default")
@@ -38,6 +39,8 @@ def clean_chat_log_job():
3839
if count == 0:
3940
break
4041
deleted_count, _ = Chat.objects.filter(id__in=logs_to_delete).delete()
42+
# 删除对应的文件
43+
File.objects.filter(~Q(meta__chat_id__in=logs_to_delete)).delete()
4144
if deleted_count < batch_size:
4245
break
4346

apps/common/job/clean_orphaned_file_job.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

apps/dataset/models/data_set.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import uuid
1010

1111
from django.db import models
12+
from django.db.models.signals import pre_delete
13+
from django.dispatch import receiver
1214

1315
from common.db.sql_execute import select_one
1416
from common.mixins.app_model_mixin import AppModelMixin
@@ -157,3 +159,9 @@ def save(
157159
def get_byte(self):
158160
result = select_one(f'SELECT lo_get({self.loid}) as "data"', [])
159161
return result['data']
162+
163+
164+
165+
@receiver(pre_delete, sender=File)
166+
def on_delete_file(sender, instance, **kwargs):
167+
select_one(f'SELECT lo_unlink({instance.loid})', [])

0 commit comments

Comments
 (0)