-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_simple.py
More file actions
178 lines (148 loc) · 5.57 KB
/
Copy pathrun_simple.py
File metadata and controls
178 lines (148 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
"""
简版工具独立启动入口
单模型(智谱)快速生成简历定制工具。
访问: http://localhost:5001
启动模式(通过环境变量 FLASK_ENV 设置):
- 开发模式: FLASK_ENV=development (自动重载、详细错误、全量清缓存)
- 应用模式: FLASK_ENV=production 或不设置 (稳定运行、清过期缓存)
"""
import os
import time
import logging
import shutil
from pathlib import Path
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def clear_pycache(project_root: Path) -> int:
"""清理所有 __pycache__ 目录"""
count = 0
try:
for pycache in project_root.rglob("__pycache__"):
try:
shutil.rmtree(pycache)
count += 1
except Exception:
pass
except FileNotFoundError:
pass
except Exception as e:
logger.warning(f"清理 __pycache__ 时出错: {e}")
return count
def clear_business_cache(project_root: Path) -> int:
"""清理 cache/ 目录下的业务缓存文件"""
cache_dir = project_root / 'cache'
count = 0
if not cache_dir.exists():
return count
for cache_file in cache_dir.glob("*.json"):
try:
cache_file.unlink()
count += 1
except Exception as e:
logger.warning(f"删除缓存文件失败 {cache_file}: {e}")
return count
def clear_old_storage_files(project_root: Path, retention_days: int = 30) -> int:
"""清理 storage/uploads/ 和 storage/tailored/ 下超过 retention_days 天的目录"""
threshold = time.time() - retention_days * 86400
count = 0
for storage_subdir in ['storage/uploads', 'storage/tailored']:
storage_dir = project_root / storage_subdir
if not storage_dir.exists():
continue
for user_dir in storage_dir.iterdir():
if not user_dir.is_dir():
continue
for session_dir in user_dir.iterdir():
if not session_dir.is_dir():
continue
try:
if session_dir.stat().st_mtime < threshold:
shutil.rmtree(session_dir)
count += 1
except Exception as e:
logger.warning(f"清理 storage 失败 {session_dir}: {e}")
return count
def clear_all_caches(is_development: bool):
"""统一清理所有缓存
Args:
is_development: 是否开发模式(开发模式清理更彻底)
"""
project_root = Path(__file__).parent
logger.info("清理缓存...")
# 1. 清理 __pycache__(所有模式都清理)
pycache_count = clear_pycache(project_root)
if pycache_count > 0:
logger.info(f" __pycache__: 已清理 {pycache_count} 个目录")
# 2. 清理业务缓存 cache/
if is_development:
biz_count = clear_business_cache(project_root)
if biz_count > 0:
logger.info(f" cache/: 已清理 {biz_count} 个文件(全量)")
else:
try:
from core.cache_manager import CacheManager
cm = CacheManager()
biz_count = cm.clear_all()
if biz_count > 0:
logger.info(f" cache/: 已清理 {biz_count} 个缓存文件(全量)")
except Exception as e:
logger.warning(f" cache/: 清理失败 {e}")
# 3. 清理过期 storage 文件(所有模式都清理)
try:
from core.config import config
retention_days = config.HISTORY_RETENTION_DAYS
except Exception:
retention_days = 30
storage_count = clear_old_storage_files(project_root, retention_days)
if storage_count > 0:
logger.info(f" storage/: 已清理 {storage_count} 个过期目录(保留 {retention_days} 天)")
# 4. 清理数据库过期数据(所有模式都清理)
try:
from core.database import db
db_count = db.cleanup_expired()
if db_count > 0:
logger.info(f" 数据库: 已清理 {db_count} 条过期记录")
except Exception as e:
logger.warning(f" 数据库: 清理失败 {e}")
logger.info("缓存清理完成")
if __name__ == '__main__':
from core.config import config
# 判断运行模式
flask_env = os.getenv('FLASK_ENV', 'production').lower()
is_development = flask_env == 'development'
# 所有模式:启动时统一清理缓存
clear_all_caches(is_development)
# 验证配置
try:
config.validate()
except ValueError as e:
logger.error(f"配置验证失败: {e}")
print(f"错误: {e}")
print("请确保已设置 ZHIPU_API_KEY 环境变量或 .env 文件")
exit(1)
# 创建并启动应用
from apps.simple_app import create_app
app = create_app()
port = config.SIMPLE_APP_PORT
mode_label = "开发模式" if is_development else "应用模式"
print(f"\n{'='*50}")
print(f" tailorCV 简版工具 [{mode_label}]")
print(f" 访问地址: http://localhost:{port}")
print(f" 模型: 智谱AI (GLM-5)")
if is_development:
print(f" 特性: 自动重载 + 详细错误 + 全量清缓存")
else:
print(f" 特性: 稳定运行 + 过期缓存清理")
print(f"{'='*50}\n")
# 开发模式:启用自动重载;应用模式:禁用自动重载
app.run(
host='0.0.0.0',
port=port,
debug=is_development,
use_reloader=False # Windows 上 watchdog 会误检系统文件导致频繁重启
)