|
1 | 1 | # coding=utf-8 |
2 | 2 |
|
3 | | -import logging |
4 | 3 | from datetime import timedelta |
5 | 4 |
|
6 | 5 | from apscheduler.schedulers.background import BackgroundScheduler |
7 | 6 | from django.db.models import Q |
8 | 7 | from django.utils import timezone |
9 | 8 | from django_apscheduler.jobstores import DjangoJobStore |
10 | 9 |
|
11 | | -from common.utils.lock import un_lock, try_lock |
| 10 | +from common.utils.lock import un_lock, try_lock, lock |
12 | 11 | from common.utils.logger import maxkb_logger |
13 | | -from knowledge.models import File |
| 12 | +from knowledge.models import File, FileSourceType |
14 | 13 |
|
15 | 14 | scheduler = BackgroundScheduler() |
16 | 15 | scheduler.add_jobstore(DjangoJobStore(), "default") |
17 | 16 |
|
18 | 17 |
|
19 | 18 | 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(): |
20 | 24 | from django.utils.translation import gettext_lazy as _ |
21 | 25 | maxkb_logger.info(_('start clean debug file')) |
| 26 | + minutes_30_ago = timezone.now() - timedelta(minutes=30) |
22 | 27 | two_hours_ago = timezone.now() - timedelta(hours=2) |
| 28 | + one_days_ago = timezone.now() - timedelta(hours=24) |
23 | 29 | # 删除对应的文件 |
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() |
25 | 34 | maxkb_logger.info(_('end clean debug file')) |
26 | 35 |
|
27 | 36 |
|
28 | 37 | def run(): |
29 | | - if try_lock('clean_debug_file', 30 * 30): |
| 38 | + if try_lock('clean_debug_file', 30): |
30 | 39 | try: |
31 | 40 | scheduler.start() |
32 | 41 | clean_debug_file_job = scheduler.get_job(job_id='clean_debug_file') |
33 | 42 | if clean_debug_file_job is not None: |
34 | 43 | 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') |
36 | 45 | finally: |
37 | 46 | un_lock('clean_debug_file') |
0 commit comments