forked from yuetianle-Media/mediamaster-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
325 lines (273 loc) · 11.1 KB
/
Copy pathmain.py
File metadata and controls
325 lines (273 loc) · 11.1 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import os
import json
import subprocess
import time
import logging
import sys
import signal
import sqlite3
import psutil
import threading
# 配置日志
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("/tmp/log/main.log", mode='w'),
logging.StreamHandler()
]
)
def get_run_interval_from_db():
try:
conn = sqlite3.connect('/config/data.db')
cursor = conn.cursor()
cursor.execute("SELECT VALUE FROM CONFIG WHERE OPTION = 'run_interval_hours';")
result = cursor.fetchone()
cursor.close()
conn.close()
if result:
return int(result[0])
else:
logging.warning("未找到 run_interval_hours 配置项,使用默认值 6 小时。")
return 6
except Exception as e:
logging.error(f"无法从数据库读取 run_interval_hours: {e},使用默认值 6 小时。")
return 6
def run_script(script_name):
try:
result = subprocess.run(['python', script_name], check=True)
logging.debug(f"{script_name} 已执行完毕。")
except subprocess.CalledProcessError as e:
logging.error(f"{script_name} 执行失败,退出程序。错误信息: {e}")
sys.exit(0)
def start_app():
try:
with open(os.devnull, 'w') as devnull:
process = subprocess.Popen(['python', 'app.py'], stdout=devnull, stderr=devnull)
logging.info("WEB管理已启动。")
return process.pid
except Exception as e:
logging.error(f"无法启动WEB管理程序: {e}")
sys.exit(0)
def start_sync():
def delayed_start():
try:
# 延时2分钟启动
time.sleep(120)
process = subprocess.Popen(['python', 'sync.py'])
logging.info("目录监控服务已启动。")
# 保存进程ID供后续使用
global sync_pid
sync_pid = process.pid
except Exception as e:
logging.error(f"无法启动目录监控服务: {e}")
# 使用线程启动延时任务,不阻塞主线程
thread = threading.Thread(target=delayed_start, daemon=True)
thread.start()
logging.info("目录监控服务将在2分钟后启动...")
# 返回None,因为实际的PID会在延时后设置
return None
def start_xunlei_torrent():
try:
process = subprocess.Popen(['python', 'xunlei_torrent.py'])
logging.info("迅雷-种子监听服务已启动。")
return process.pid
except Exception as e:
logging.error(f"无法启动迅雷-种子监听服务: {e}")
sys.exit(0)
def start_check_db_dir():
try:
process = subprocess.Popen(['python', 'check_db_dir.py'])
logging.info("启动数据库和目录检查服务")
return process.pid
except Exception as e:
logging.error(f"无法启动数据库和目录检查服务: {e}")
sys.exit(0)
def report_versions():
try:
process = subprocess.Popen(['python', 'report_versions.py'])
logging.info("启动版本检测及统计服务")
return process.pid
except Exception as e:
logging.error(f"无法启动版本检测及统计服务: {e}")
sys.exit(0)
def monitor_chrome_process():
chrome_started_time = None
chromedriver_started_time = None
for proc in psutil.process_iter(['pid', 'name', 'create_time']):
try:
process_name = proc.info['name'].lower()
create_time = proc.info['create_time']
if 'chrome' in process_name:
if chrome_started_time is None or create_time < chrome_started_time:
chrome_started_time = create_time
if 'chromedriver' in process_name:
if chromedriver_started_time is None or create_time < chromedriver_started_time:
chromedriver_started_time = create_time
except psutil.NoSuchProcess:
continue
def terminate_process(process_name_filter, started_time, threshold_seconds, log_prefix):
if started_time:
run_time = time.time() - started_time
if run_time > threshold_seconds:
logging.warning(f"{log_prefix} 进程已运行超过 {threshold_seconds // 60} 分钟,判定为异常,正在终止。")
for proc in psutil.process_iter(['pid', 'name']):
try:
if process_name_filter in proc.info['name'].lower():
p = psutil.Process(proc.info['pid'])
p.terminate()
logging.info(f"已终止 {log_prefix} 进程 PID: {proc.info['pid']}")
except psutil.NoSuchProcess:
pass
# 监控 Chrome 进程
terminate_process('chrome', chrome_started_time, 20 * 60, "Chrome")
# 监控 Chromedriver 进程
terminate_process('chromedriver', chromedriver_started_time, 20 * 60, "Chromedriver")
def kill_zombie_processes():
"""
检测并记录僵尸进程
僵尸进程只能由其父进程清理,这里仅做记录
"""
zombie_count = 0
for proc in psutil.process_iter(['pid', 'name', 'status']):
try:
if proc.info['status'] == psutil.STATUS_ZOMBIE:
zombie_count += 1
logging.debug(f"检测到僵尸进程 PID: {proc.info['pid']}, NAME: {proc.info['name']}")
except (psutil.NoSuchProcess, psutil.AccessDenied, KeyError):
# 忽略进程已不存在、无访问权限或键不存在的情况
pass
if zombie_count > 0:
logging.info(f"检测到 {zombie_count} 个僵尸进程,等待系统自动清理")
# 调用清理僵尸进程函数
kill_zombie_processes()
def chrome_monitor_thread():
while running:
monitor_chrome_process()
time.sleep(300) # 每5分钟检查一次
def start_chrome_monitor():
thread = threading.Thread(target=chrome_monitor_thread, daemon=True)
thread.start()
logging.info("Chrome 进程监控已启动")
def check_site_status_and_save():
"""检查站点状态并保存到文件"""
try:
# 导入站点测试模块
import sys
sys.path.append('/app')
import site_test
# 运行站点测试
tester = site_test.SiteTester()
results = tester.run_tests()
# 保存结果到文件
status_data = {
'status': results,
'last_checked': time.strftime('%Y-%m-%d %H:%M:%S')
}
with open('/tmp/site_status.json', 'w', encoding='utf-8') as f:
json.dump(status_data, f, ensure_ascii=False, indent=2)
logging.info("站点状态检测完成并已保存到 /tmp/site_status.json")
except Exception as e:
logging.error(f"站点状态检测失败: {e}")
# 全局变量
running = True
app_pid = None
sync_pid = None
xunlei_started = False
# 信号处理函数
def shutdown_handler(signum, frame):
global running, app_pid, sync_pid
logging.info(f"收到信号 {signum},正在关闭程序...")
running = False
if app_pid:
logging.info(f"终止 app.py 进程 (PID: {app_pid})")
try:
os.kill(app_pid, signal.SIGTERM)
except ProcessLookupError:
logging.warning(f"进程 {app_pid} 不存在,跳过终止操作。")
if sync_pid:
logging.info(f"终止 sync.py 进程 (PID: {sync_pid})")
try:
os.kill(sync_pid, signal.SIGTERM)
except ProcessLookupError:
logging.warning(f"进程 {sync_pid} 不存在,跳过终止操作。")
time.sleep(5)
logging.info("程序已关闭。")
sys.exit(0)
signal.signal(signal.SIGTERM, shutdown_handler)
signal.signal(signal.SIGINT, shutdown_handler)
def main():
global app_pid, sync_pid, running, xunlei_started
run_interval_hours = get_run_interval_from_db()
run_interval_seconds = run_interval_hours * 3600
app_pid = start_app()
sync_pid = start_sync()
start_chrome_monitor() # 启动 Chrome 监控线程
while running:
# 在主循环中定期执行站点状态检测
check_site_status_and_save()
logging.info("-" * 80)
logging.info("站点状态检测:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('scan_media.py')
logging.info("-" * 80)
logging.info("扫描媒体库:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('subscr.py')
logging.info("-" * 80)
logging.info("获取最新豆瓣订阅:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('check_subscr.py')
logging.info("-" * 80)
logging.info("检查是否有新增订阅:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('indexer.py')
logging.info("-" * 80)
logging.info("建立订阅资源索引:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('downloader.py')
logging.info("-" * 80)
logging.info("下载订阅媒体资源:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
if not xunlei_started:
start_xunlei_torrent()
xunlei_started = True
run_script('tmdb_id.py')
logging.info("-" * 80)
logging.info("更新数据库TMDB_ID:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('dateadded.py')
logging.info("-" * 80)
logging.info("更新媒体NFO文件添加日期:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('actor_nfo.py')
logging.info("-" * 80)
logging.info("更新演职人员中文信息:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('episodes_nfo.py')
logging.info("-" * 80)
logging.info("更新集演职人员中文信息:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
run_script('auto_delete_tasks.py')
logging.info("-" * 80)
logging.info("自动删除已完成做种任务:已执行完毕,等待5秒...")
logging.info("-" * 80)
time.sleep(5)
logging.info(f"所有任务已完成,等待 {run_interval_hours} 小时后再次运行...")
time.sleep(run_interval_seconds)
if __name__ == "__main__":
start_check_db_dir()
report_versions()
logging.info("等待初始化检查...")
time.sleep(8)
main()