Qilan2 commited on
Commit
4a1df67
·
verified ·
1 Parent(s): 624311b

Update server2.py

Browse files
Files changed (1) hide show
  1. server2.py +50 -46
server2.py CHANGED
@@ -395,85 +395,89 @@ port = {port}
395
 
396
  # write_rsyncd_conf(data_folder)
397
  # os.system("ps aux | grep rsync")
 
 
 
 
 
 
 
 
 
398
  def check_api1():
399
  # 第一次检查 SillyTavern 是否启动
400
- thread_started = False # 在函数内初始化标志变量
401
- n=0
402
  while True:
403
- n = n + 1
404
  if not os.path.exists(data_folder):
405
- print(f"/sillytavern/ 文件不存在,结束检测")
406
  break
407
  try:
408
  url = "http://127.0.0.1:8000/v1"
409
  response = requests.get(url, timeout=30)
410
 
411
- # 检查响应状态码
412
  if response.status_code == 403:
413
- print("SillyTavern已经启动")
414
- # threading.Thread(target=Oneapi, daemon=True).start()
415
- break # 接口正常,退出循环
416
 
417
- # 如果不是 403,打印其他信息
418
- print("SillyTavern未启动,状态码:", response.status_code,n)
419
 
420
  except requests.exceptions.RequestException as e:
421
- print(f"SillyTavern未启动3",n) # 打印具体错误信息
422
 
423
- time.sleep(5) # 等待5秒后重试
424
 
425
- print('开始检测接口状态')
426
  Service_status = False
427
- # 第二次检查接口状态
428
  n = 0
429
- nn=0
430
  while True:
431
- n = n + 1
432
- if not os.path.exists(data_folder):#
433
- print(f"/sillytavern/ 文件不存在,结束检测")
434
  break
435
  if Service_status:
436
- print("结束检测")
437
  break
438
- url = f"{st_url}/csrf-token" # 确保这个 URL 是正确的
439
  try:
440
  response = requests_retry_session().get(url, timeout=30)
441
- response.raise_for_status() # 检查响应状态
442
- # 尝试解析 JSON 响应
443
- response_json = response.json() # 获取 JSON 内容
444
- print(url,"接口返回信息:", response_json)
445
  if "token" in response_json:
446
- print("检测到 token")
447
  nn = 0
448
- try:
449
- # rsync -avz --port=7073 ljj@8.137.94.137::qilan /a/
450
- # subprocess.run(f"rsync -avz --port=7075 ljj@{frp_url}::qilan {data_folder}/", shell=True, check=True)
451
- subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/", shell=True, check=True, timeout=600)
452
- except subprocess.CalledProcessError as e:
453
- print(f"Rsync 失败,返回码: {e.returncode}")
454
- print(f"Command output: {e.output}")
455
- generate_ssh_key()
456
- # break # 跳出循环
 
 
 
457
  else:
458
- print("接口返回信息:", response_json)
459
  except requests.exceptions.ConnectionError as e:
460
- print(f"接口异常1",n,nn)
461
- nn = nn + 1
462
  if nn > 10:
463
  break
464
- # threading.Thread(target=Frpc, daemon=True).start()
465
-
466
  except requests.exceptions.Timeout as e:
467
- print(f"接口异常2",n)
468
- # threading.Thread(target=Frpc, daemon=True).start()
469
  break
470
-
471
  except requests.exceptions.RequestException as e:
472
- print(f"接口异常3:",n)
473
- # threading.Thread(target=Frpc, daemon=True).start()
474
- break
475
 
476
- time.sleep(2) # 等待5秒后重试
 
 
477
 
478
 
479
  def frp(configs, backstage):
 
395
 
396
  # write_rsyncd_conf(data_folder)
397
  # os.system("ps aux | grep rsync")
398
+ import os
399
+ import time
400
+ import requests
401
+ import subprocess
402
+ import logging
403
+
404
+ # 配置日志
405
+ logging.basicConfig(level=logging.INFO)
406
+
407
  def check_api1():
408
  # 第一次检查 SillyTavern 是否启动
409
+ thread_started = False
410
+ n = 0
411
  while True:
412
+ n += 1
413
  if not os.path.exists(data_folder):
414
+ logging.error(f"/sillytavern/ 文件不存在,结束检测")
415
  break
416
  try:
417
  url = "http://127.0.0.1:8000/v1"
418
  response = requests.get(url, timeout=30)
419
 
 
420
  if response.status_code == 403:
421
+ logging.info("SillyTavern已经启动")
422
+ break
 
423
 
424
+ logging.info("SillyTavern未启动,状态码:", response.status_code, n)
 
425
 
426
  except requests.exceptions.RequestException as e:
427
+ logging.error(f"SillyTavern未启动3: {e}, 尝试次数: {n}")
428
 
429
+ time.sleep(5)
430
 
431
+ logging.info('开始检测接口状态')
432
  Service_status = False
 
433
  n = 0
434
+ nn = 0
435
  while True:
436
+ n += 1
437
+ if not os.path.exists(data_folder):
438
+ logging.error(f"/sillytavern/ 文件不存在,结束检测")
439
  break
440
  if Service_status:
441
+ logging.info("结束检测")
442
  break
443
+ url = f"{st_url}/csrf-token"
444
  try:
445
  response = requests_retry_session().get(url, timeout=30)
446
+ response.raise_for_status()
447
+ response_json = response.json()
448
+ logging.info(url, "接口返回信息:", response_json)
 
449
  if "token" in response_json:
450
+ logging.info("检测到 token")
451
  nn = 0
452
+ retry_count = 3 # 设置重试次数
453
+ for attempt in range(retry_count):
454
+ try:
455
+
456
+ subprocess.run(f"rsync -avz -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/",
457
+ shell=True, check=True, timeout=600)
458
+ break # 如果成功,跳出重试循环
459
+ except subprocess.CalledProcessError as e:
460
+ logging.error(f"Rsync 失败,返回码: {e.returncode}, 尝试次数: {attempt + 1}")
461
+ if attempt == retry_count - 1:
462
+ logging.error("所有重试均失败,停止尝试。")
463
+ generate_ssh_key() # 处理 SSH 密钥生成
464
  else:
465
+ logging.info("接口返回信息:", response_json)
466
  except requests.exceptions.ConnectionError as e:
467
+ logging.error(f"接口异常1: {e}, 尝试次数: {n}, nn: {nn}")
468
+ nn += 1
469
  if nn > 10:
470
  break
 
 
471
  except requests.exceptions.Timeout as e:
472
+ logging.error(f"接口异常2: {e}, 尝试次数: {n}")
 
473
  break
 
474
  except requests.exceptions.RequestException as e:
475
+ logging.error(f"接口异常3: {e}, 尝试次数: {n}")
476
+ break
 
477
 
478
+ time.sleep(2)
479
+
480
+
481
 
482
 
483
  def frp(configs, backstage):