101 lines
3.7 KiB
Python
101 lines
3.7 KiB
Python
import os
|
|
import shutil
|
|
import threading
|
|
import time
|
|
import logging
|
|
from shared_utils import file_registry, file_lock, task_registry, task_lock
|
|
|
|
UPLOAD_FOLDER = 'temp_files'
|
|
FRAMES_FOLDER = 'frames' # 视频帧提取任务文件夹
|
|
CLEANUP_INTERVAL = 3600 # 清理临时文件的间隔(秒)
|
|
FILE_EXPIRY = 7200 # 文件过期时间(秒)
|
|
TASK_EXPIRY = 7200 # 任务记录过期时间(秒)
|
|
|
|
def cleanup_temp_files():
|
|
current_time = time.time()
|
|
expired_folders = []
|
|
|
|
# 清理temp_files目录
|
|
if os.path.exists(UPLOAD_FOLDER):
|
|
for folder_name in os.listdir(UPLOAD_FOLDER):
|
|
folder_path = os.path.join(UPLOAD_FOLDER, folder_name)
|
|
|
|
if not os.path.isdir(folder_path):
|
|
continue
|
|
|
|
try:
|
|
folder_mtime = os.path.getmtime(folder_path)
|
|
|
|
if current_time - folder_mtime > FILE_EXPIRY:
|
|
expired_folders.append(folder_path)
|
|
logging.info(f"检测到过期文件夹: {folder_path} (修改时间: {time.ctime(folder_mtime)})")
|
|
except Exception as e:
|
|
logging.error(f"获取文件夹 {folder_path} 信息失败: {e}")
|
|
|
|
# 清理frames目录
|
|
if os.path.exists(FRAMES_FOLDER):
|
|
for folder_name in os.listdir(FRAMES_FOLDER):
|
|
folder_path = os.path.join(FRAMES_FOLDER, folder_name)
|
|
|
|
if not os.path.isdir(folder_path):
|
|
continue
|
|
|
|
try:
|
|
folder_mtime = os.path.getmtime(folder_path)
|
|
|
|
if current_time - folder_mtime > FILE_EXPIRY:
|
|
expired_folders.append(folder_path)
|
|
logging.info(f"检测到过期视频帧文件夹: {folder_path} (修改时间: {time.ctime(folder_mtime)})")
|
|
except Exception as e:
|
|
logging.error(f"获取视频帧文件夹 {folder_path} 信息失败: {e}")
|
|
|
|
# 删除所有过期文件夹
|
|
for folder_path in expired_folders:
|
|
try:
|
|
shutil.rmtree(folder_path)
|
|
logging.info(f"已删除过期文件夹: {folder_path}")
|
|
except Exception as e:
|
|
logging.error(f"删除文件夹 {folder_path} 失败: {e}")
|
|
|
|
logging.info(f"清理完成,共删除 {len(expired_folders)} 个过期文件夹")
|
|
|
|
# 清理过期的任务记录和文件记录
|
|
cleanup_expired_registries(current_time)
|
|
|
|
def cleanup_expired_registries(current_time):
|
|
"""清理过期的任务记录和文件记录"""
|
|
# 清理过期的任务记录
|
|
with task_lock:
|
|
expired_tasks = []
|
|
for task_id, task_info in list(task_registry.items()):
|
|
if current_time - task_info['create_time'] > TASK_EXPIRY:
|
|
expired_tasks.append(task_id)
|
|
|
|
for task_id in expired_tasks:
|
|
del task_registry[task_id]
|
|
logging.info(f"已清理过期任务记录: {task_id}")
|
|
|
|
# 清理过期的文件记录(基于最后访问时间)
|
|
with file_lock:
|
|
expired_files = []
|
|
for file_id, file_info in list(file_registry.items()):
|
|
if current_time - file_info['last_access'] > FILE_EXPIRY:
|
|
expired_files.append(file_id)
|
|
|
|
for file_id in expired_files:
|
|
del file_registry[file_id]
|
|
logging.info(f"已清理过期文件记录: {file_id}")
|
|
|
|
logging.info(f"共清理 {len(expired_tasks)} 个任务记录和 {len(expired_files)} 个文件记录")
|
|
|
|
def start_cleanup_thread():
|
|
def cleanup_loop():
|
|
while True:
|
|
logging.info("开始执行定期清理任务...")
|
|
cleanup_temp_files()
|
|
time.sleep(CLEANUP_INTERVAL)
|
|
|
|
thread = threading.Thread(target=cleanup_loop)
|
|
thread.daemon = True
|
|
thread.start()
|
|
logging.info("已启动后台清理线程") |