Update Card_Hub.py
Browse files- Card_Hub.py +57 -49
Card_Hub.py
CHANGED
|
@@ -349,30 +349,7 @@ def check_api2():
|
|
| 349 |
|
| 350 |
time.sleep(5)
|
| 351 |
def check_api1():
|
| 352 |
-
|
| 353 |
-
# thread_started = False
|
| 354 |
-
# n = 0
|
| 355 |
-
# while True:
|
| 356 |
-
# n += 1
|
| 357 |
-
# if not os.path.exists('/Card_Hub'):
|
| 358 |
-
# logging.error(f"/Card_Hub/ 文件不存在,结束检测")
|
| 359 |
-
# break
|
| 360 |
-
# try:
|
| 361 |
-
# url = "http://localhost:8003"
|
| 362 |
-
# response = requests.get(url, timeout=30)
|
| 363 |
-
|
| 364 |
-
# if response.status_code ==200:
|
| 365 |
-
# print("Card_Hub已经启动")
|
| 366 |
-
# break
|
| 367 |
-
|
| 368 |
-
# print("Card_Hub未启动,状态码:", response.status_code, n)
|
| 369 |
-
|
| 370 |
-
# except requests.exceptions.RequestException as e:
|
| 371 |
-
# print(f"Card_Hub未启动3: {e}, 尝试次数: {n}")
|
| 372 |
-
|
| 373 |
-
# time.sleep(5)
|
| 374 |
-
|
| 375 |
-
# print('Card_Hub本地已经运行,开始检测接口状态')
|
| 376 |
Service_status = False
|
| 377 |
n = 0
|
| 378 |
nn = 0
|
|
@@ -386,33 +363,41 @@ def check_api1():
|
|
| 386 |
break
|
| 387 |
if nn > 2:
|
| 388 |
break
|
|
|
|
| 389 |
url = f"{st_url}/api"
|
| 390 |
try:
|
| 391 |
response = requests_retry_session().get(url, timeout=30)
|
| 392 |
response.raise_for_status()
|
| 393 |
response_json = response.json()
|
| 394 |
-
print("
|
| 395 |
if "message" in response_json:
|
| 396 |
-
print("
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
except Exception as e:
|
| 400 |
-
print(f"同步过程出错: {e}")
|
| 401 |
-
nn += 1
|
| 402 |
else:
|
| 403 |
-
print("
|
|
|
|
| 404 |
except requests.exceptions.ConnectionError as e:
|
| 405 |
-
print(f"
|
| 406 |
-
# time.sleep(3)
|
| 407 |
nn += 1
|
| 408 |
if nn > 5:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
break
|
| 410 |
except requests.exceptions.Timeout as e:
|
| 411 |
-
print(f"
|
| 412 |
-
|
| 413 |
except requests.exceptions.RequestException as e:
|
| 414 |
-
print(f"
|
| 415 |
-
|
| 416 |
|
| 417 |
time.sleep(5)
|
| 418 |
|
|
@@ -566,22 +551,45 @@ def frp(configs, backstage):
|
|
| 566 |
|
| 567 |
|
| 568 |
def monitor_port():
|
|
|
|
| 569 |
while True:
|
| 570 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 571 |
result = sock.connect_ex(('localhost', 8003))
|
| 572 |
if result == 0:
|
| 573 |
-
print(f"
|
| 574 |
else:
|
| 575 |
-
print(f"
|
| 576 |
-
#
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
time.sleep(5) # 每5秒检查一次
|
| 587 |
def Run_SillyTavern():
|
|
|
|
| 349 |
|
| 350 |
time.sleep(5)
|
| 351 |
def check_api1():
|
| 352 |
+
"""检查源服务器状态并决定是否启动本地服务"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
Service_status = False
|
| 354 |
n = 0
|
| 355 |
nn = 0
|
|
|
|
| 363 |
break
|
| 364 |
if nn > 2:
|
| 365 |
break
|
| 366 |
+
|
| 367 |
url = f"{st_url}/api"
|
| 368 |
try:
|
| 369 |
response = requests_retry_session().get(url, timeout=30)
|
| 370 |
response.raise_for_status()
|
| 371 |
response_json = response.json()
|
| 372 |
+
print(f"{url} 接口返回信息: {response_json}")
|
| 373 |
if "message" in response_json:
|
| 374 |
+
print("源服务器正常运行,无需启动本地服务")
|
| 375 |
+
time.sleep(300) # 每5分钟检查一次
|
| 376 |
+
continue
|
|
|
|
|
|
|
|
|
|
| 377 |
else:
|
| 378 |
+
print(f"检测到异常 {url} 接口返回信息: {response_json}")
|
| 379 |
+
nn += 1
|
| 380 |
except requests.exceptions.ConnectionError as e:
|
| 381 |
+
print(f"源服务器连接失败: {e}, 尝试次数: {n}, nn: {nn}")
|
|
|
|
| 382 |
nn += 1
|
| 383 |
if nn > 5:
|
| 384 |
+
print("源服务器多次连接失败,准备启动本地服务...")
|
| 385 |
+
try:
|
| 386 |
+
# 启动本地服务
|
| 387 |
+
Run_SillyTavern()
|
| 388 |
+
# 启动 FRP
|
| 389 |
+
frp(configs, True)
|
| 390 |
+
Service_status = True
|
| 391 |
+
break
|
| 392 |
+
except Exception as e:
|
| 393 |
+
print(f"启动本地服务失败: {e}")
|
| 394 |
break
|
| 395 |
except requests.exceptions.Timeout as e:
|
| 396 |
+
print(f"源服务器超时: {e}, 尝试次数: {n}")
|
| 397 |
+
nn += 1
|
| 398 |
except requests.exceptions.RequestException as e:
|
| 399 |
+
print(f"源服务器请求异常: {e}, 尝试次数: {n}")
|
| 400 |
+
nn += 1
|
| 401 |
|
| 402 |
time.sleep(5)
|
| 403 |
|
|
|
|
| 551 |
|
| 552 |
|
| 553 |
def monitor_port():
|
| 554 |
+
"""监控本地端口状态"""
|
| 555 |
while True:
|
| 556 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
| 557 |
result = sock.connect_ex(('localhost', 8003))
|
| 558 |
if result == 0:
|
| 559 |
+
print(f"本地服务端口 8003 正在使用中")
|
| 560 |
else:
|
| 561 |
+
print(f"本地服务端口 8003 未被使用")
|
| 562 |
+
# 检查源服务器状态
|
| 563 |
+
try:
|
| 564 |
+
response = requests.get(f"{st_url}/api", timeout=5)
|
| 565 |
+
if response.status_code == 200:
|
| 566 |
+
print("源服务器正常运行,无需启动本地服务")
|
| 567 |
+
else:
|
| 568 |
+
print("源服务器异常,准备启动本地服务...")
|
| 569 |
+
# 读取 config.ini 中的 monitor_port_enabled 配置项
|
| 570 |
+
config = configparser.ConfigParser()
|
| 571 |
+
config.read('/config.ini')
|
| 572 |
+
monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 573 |
+
if monitor_port_enabled:
|
| 574 |
+
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 575 |
+
backup_thread.start()
|
| 576 |
+
# 启动 FRP
|
| 577 |
+
frp(configs, True)
|
| 578 |
+
else:
|
| 579 |
+
print(f"monitor_port_enabled 为False跳过")
|
| 580 |
+
except requests.exceptions.RequestException:
|
| 581 |
+
print("源服务器无法访问,准备启动本地服务...")
|
| 582 |
+
# 读取 config.ini 中的 monitor_port_enabled 配置项
|
| 583 |
+
config = configparser.ConfigParser()
|
| 584 |
+
config.read('/config.ini')
|
| 585 |
+
monitor_port_enabled = config.getboolean('general', 'monitor_port_enabled', fallback=False)
|
| 586 |
+
if monitor_port_enabled:
|
| 587 |
+
backup_thread = threading.Thread(target=Run_SillyTavern, daemon=True)
|
| 588 |
+
backup_thread.start()
|
| 589 |
+
# 启动 FRP
|
| 590 |
+
frp(configs, True)
|
| 591 |
+
else:
|
| 592 |
+
print(f"monitor_port_enabled 为False跳过")
|
| 593 |
|
| 594 |
time.sleep(5) # 每5秒检查一次
|
| 595 |
def Run_SillyTavern():
|