51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
"""定时维护命令。
|
|
|
|
用法:
|
|
python manage.py maintenance # 同 --once
|
|
python manage.py maintenance --once # 执行一轮(清理过期任务/临时文件、按需备份)
|
|
python manage.py maintenance --loop # 常驻:启动即一轮,之后每小时醒来检查
|
|
"""
|
|
import time
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from rdgenerator import maintenance
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = '清理过期构建任务与临时文件,并按设置定期备份 SQLite 数据库。'
|
|
|
|
LOOP_INTERVAL = 3600 # 每小时醒来一次(备份内部按 24h 间隔节流)
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
'--loop', action='store_true',
|
|
help='常驻模式:容器启动后在后台持续运行。',
|
|
)
|
|
parser.add_argument(
|
|
'--once', action='store_true',
|
|
help='只执行一轮后退出(默认行为,也供后台「立即执行」使用)。',
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
if options['loop']:
|
|
self.stdout.write('[maintenance] 守护进程已启动,每小时检查一次。')
|
|
while True:
|
|
self._run_once()
|
|
time.sleep(self.LOOP_INTERVAL)
|
|
else:
|
|
self._run_once()
|
|
|
|
def _run_once(self):
|
|
result = maintenance.run_maintenance()
|
|
self.stdout.write(
|
|
'[maintenance] 清理过期任务 {deleted_builds} 条、临时文件 {deleted_temp_files} 个、'
|
|
'轮转旧备份 {rotated_backups} 份;备份:{backup}'.format(
|
|
deleted_builds=result['deleted_builds'],
|
|
deleted_temp_files=result['deleted_temp_files'],
|
|
rotated_backups=result['rotated_backups'],
|
|
backup=('已创建 ' + result['backup_created']) if result['backup_created']
|
|
else ('本轮跳过' if result['backup_skipped'] else '未创建'),
|
|
)
|
|
)
|