@@ -23,15 +23,29 @@ def clean_chat_log_job_lock():
2323 maxkb_logger .info (_ ('start clean chat log' ))
2424 now = timezone .now ()
2525
26- applications = Application .objects .all ().values ('id' , 'clean_time' )
26+ applications = Application .objects .all ().values ('id' , 'clean_time' , 'file_clean_time' )
2727 cutoff_dates = {
2828 app ['id' ]: now - datetime .timedelta (days = app ['clean_time' ] or 180 )
2929 for app in applications
3030 }
31+ file_cutoff_dates = {
32+ app ['id' ]: now - datetime .timedelta (days = app ['file_clean_time' ] or app ['clean_time' ] or 180 )
33+ for app in applications
34+ }
35+ file_conditions = Q ()
36+ for app_id , cutoff_date in file_cutoff_dates .items ():
37+ file_conditions |= Q (chat__application_id = app_id , create_time__lt = cutoff_date )
38+ clean_method (file_conditions , clean_log = False )
3139
3240 query_conditions = Q ()
3341 for app_id , cutoff_date in cutoff_dates .items ():
3442 query_conditions |= Q (chat__application_id = app_id , create_time__lt = cutoff_date )
43+ clean_method (query_conditions )
44+
45+ maxkb_logger .info (_ ('end clean chat log' ))
46+
47+
48+ def clean_method (query_conditions , clean_log = True ):
3549 batch_size = 500
3650 while True :
3751 with transaction .atomic ():
@@ -54,20 +68,20 @@ def clean_chat_log_job_lock():
5468 (item ['max_create_time' ] for item in max_create_times if item ['chat_id' ] == record .chat_id ), None )
5569 if max_create_time :
5670 files_to_delete .extend (
57- File .objects .filter (meta__chat_id = str (record .chat_id ), create_time__lt = max_create_time )
71+ File .objects .filter (source_id = str (record .chat_id ), create_time__lt = max_create_time )
5872 )
5973 # 删除 ChatRecord
60- deleted_count = ChatRecord .objects .filter (id__in = chat_record_ids ).delete ()[0 ]
74+ deleted_count = 0
75+ if clean_log :
76+ deleted_count = ChatRecord .objects .filter (id__in = chat_record_ids ).delete ()[0 ]
6177
62- # 删除没有关联 ChatRecord 的 Chat
63- Chat .objects .filter (chatrecord__isnull = True , id__in = chat_ids ).delete ()
78+ # 删除没有关联 ChatRecord 的 Chat
79+ Chat .objects .filter (chatrecord__isnull = True , id__in = chat_ids ).delete ()
6480 File .objects .filter (loid__in = [file .loid for file in files_to_delete ]).delete ()
6581
6682 if deleted_count < batch_size :
6783 break
6884
69- maxkb_logger .info (_ ('end clean chat log' ))
70-
7185
7286def run ():
7387 rlock = RedisLock ()
0 commit comments