fix: 启动时自动清理残留的临时解压目录

This commit is contained in:
向宇康 committed 2026-04-14 15:45:00 +08:00
1 parent 1410c55a3b
commit 802331bc7c
1 file changed
+29
+29
View File
@@ -187,6 +187,9 @@ def create_app(config_name):
response.headers['Cache-Control'] = 'public, max-age=31536000'
return response
# 启动时清理残留的临时目录
_cleanup_temp_dirs(app)
return app
@@ -275,3 +278,29 @@ def init_scheduler(app):
app.logger.warning("APScheduler 未安装,自动更新功能不可用")
except Exception as e:
app.logger.error(f"初始化调度器失败:{e}")
def _cleanup_temp_dirs(app):
"""启动时清理残留的临时解压目录"""
import shutil
import os
processed_root = app.config.get('PROCESSED_DATA_OUTPUT_FOLDER')
if not processed_root or not os.path.exists(processed_root):
return
cleaned_count = 0
for entry in os.listdir(processed_root):
# 匹配临时目录模式:_temp_* 或 _temp_extract_*
if entry.startswith('_temp') or entry.startswith('_temp_extract'):
entry_path = os.path.join(processed_root, entry)
if os.path.isdir(entry_path):
try:
shutil.rmtree(entry_path)
cleaned_count += 1
app.logger.info(f"清理残留临时目录: {entry_path}")
except Exception as e:
app.logger.warning(f"清理临时目录失败 {entry_path}: {e}")
if cleaned_count > 0:
app.logger.info(f"启动时清理了 {cleaned_count} 个残留临时目录")