Skip to content

Commit 1fe4c07

Browse files
committed
fix: Scheduled file scheduled deletion
1 parent 922ecb9 commit 1fe4c07

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

apps/common/job/clean_chat_job.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django_apscheduler.jobstores import DjangoJobStore
1010
from application.models import Application, Chat, ChatRecord
1111
from django.db.models import Q, Max
12-
from common.utils.lock import try_lock, un_lock
12+
from common.utils.lock import try_lock, un_lock, lock
1313
from common.utils.logger import maxkb_logger
1414

1515
from knowledge.models import File
@@ -19,6 +19,11 @@
1919

2020

2121
def clean_chat_log_job():
22+
clean_chat_log_job_lock()
23+
24+
25+
@lock(lock_key='clean_chat_log_job', timeout=30)
26+
def clean_chat_log_job_lock():
2227
from django.utils.translation import gettext_lazy as _
2328
maxkb_logger.info(_('start clean chat log'))
2429
now = timezone.now()
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
# coding=utf-8
22

3-
import logging
43
from datetime import timedelta
54

65
from apscheduler.schedulers.background import BackgroundScheduler
76
from django.db.models import Q
87
from django.utils import timezone
98
from django_apscheduler.jobstores import DjangoJobStore
109

11-
from common.utils.lock import un_lock, try_lock
10+
from common.utils.lock import un_lock, try_lock, lock
1211
from common.utils.logger import maxkb_logger
13-
from knowledge.models import File
12+
from knowledge.models import File, FileSourceType
1413

1514
scheduler = BackgroundScheduler()
1615
scheduler.add_jobstore(DjangoJobStore(), "default")
1716

1817

1918
def clean_debug_file():
19+
clean_debug_file_lock()
20+
21+
22+
@lock(lock_key='clean_debug_file', timeout=30)
23+
def clean_debug_file_lock():
2024
from django.utils.translation import gettext_lazy as _
2125
maxkb_logger.info(_('start clean debug file'))
26+
minutes_30_ago = timezone.now() - timedelta(minutes=30)
2227
two_hours_ago = timezone.now() - timedelta(hours=2)
28+
one_days_ago = timezone.now() - timedelta(hours=24)
2329
# 删除对应的文件
24-
File.objects.filter(Q(create_time__lt=two_hours_ago) & Q(meta__debug=True)).delete()
30+
File.objects.filter(
31+
Q(create_time__lt=one_days_ago, source_type=FileSourceType.TEMPORARY_1_DAY.value) |
32+
Q(create_time__lt=two_hours_ago, source_type=FileSourceType.TEMPORARY_120_MINUTE.value) |
33+
Q(create_time__lt=minutes_30_ago, source_type=FileSourceType.TEMPORARY_30_MINUTE.value)).delete()
2534
maxkb_logger.info(_('end clean debug file'))
2635

2736

2837
def run():
29-
if try_lock('clean_debug_file', 30 * 30):
38+
if try_lock('clean_debug_file', 30):
3039
try:
3140
scheduler.start()
3241
clean_debug_file_job = scheduler.get_job(job_id='clean_debug_file')
3342
if clean_debug_file_job is not None:
3443
clean_debug_file_job.remove()
35-
scheduler.add_job(clean_debug_file, 'cron', hour='2', minute='0', second='0', id='clean_debug_file')
44+
scheduler.add_job(clean_debug_file, 'cron', hour='*', minute='*/30', second='0', id='clean_debug_file')
3645
finally:
3746
un_lock('clean_debug_file')

apps/common/utils/lock.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def try_lock(key: str, timeout=None):
2121
:return: 是否获取到锁
2222
"""
2323
if timeout is None:
24-
timeout = 3600 # 默认超时时间为3600秒
24+
timeout = 3600 # 默认超时时间为3600秒
2525
return memory_cache.add(key, 'lock', timeout=timeout)
2626

2727

@@ -34,18 +34,20 @@ def un_lock(key: str):
3434
return memory_cache.delete(key)
3535

3636

37-
def lock(lock_key):
37+
def lock(lock_key, timeout=None):
3838
"""
3939
给一个函数上锁
40-
:param lock_key: 上锁key 字符串|函数 函数返回值为字符串
40+
@param lock_key: 上锁key 字符串|函数 函数返回值为字符串
41+
@param timeout: 超时时间
4142
:return: 装饰器函数 当前装饰器主要限制一个key只能一个线程去调用 相同key只能阻塞等待上一个任务执行完毕 不同key不需要等待
43+
4244
"""
4345

4446
def inner(func):
4547
def run(*args, **kwargs):
4648
key = lock_key(*args, **kwargs) if callable(lock_key) else lock_key
4749
try:
48-
if try_lock(key=key):
50+
if try_lock(key=key, timeout=timeout):
4951
return func(*args, **kwargs)
5052
finally:
5153
un_lock(key=key)

0 commit comments

Comments
 (0)