Skip to content

Commit 821ff14

Browse files
committed
perf: 首次运行会检查rcon连接状态
1 parent 413626d commit 821ff14

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

Diff for: pyinstaller.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import shutil
66
import os
77

8+
"""小心别把自己的config.ini打包进去"""
9+
810

911
def remove_ds_store(dir):
1012
# 删除DS_Store

Diff for: task_scheduler.py

+43-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self):
2323
self.rcon_command = self.conf['rcon_command']
2424
self.daemon_time = self.conf['daemon_time']
2525
self.arguments = self.conf.get('arguments', '').split()
26+
self.is_first_run = True
2627

2728
# 修改rcon源代码,忽略SessionTimeout异常
2829
def patched_run(self, command: str, *args: str, encoding: str = "utf-8") -> str:
@@ -39,10 +40,34 @@ def patched_run(self, command: str, *args: str, encoding: str = "utf-8") -> str:
3940
# Apply the monkey patch
4041
Client.run = patched_run
4142

43+
def check_rcon(self):
44+
try:
45+
with Client(
46+
host=self.conf['rcon_host'],
47+
port=self.conf['rcon_port'],
48+
passwd=self.conf['rcon_password'],
49+
timeout=1):
50+
51+
INFO.logger.info("[ RCON ] RCON连接正常")
52+
print("[ RCON ] RCON连接正常")
53+
except TimeoutError:
54+
INFO.logger.error("[ RCON ] RCON连接超时,请检查IP和端口是否填写正确")
55+
print("[ RCON ] RCON连接超时,请检查IP和端口是否填写正确")
56+
time.sleep(2)
57+
subprocess.run(['taskkill', '/f', '/im', self.appName], stderr=subprocess.DEVNULL)
58+
exit(0)
59+
except rcon.exceptions.WrongPassword:
60+
INFO.logger.error("[ RCON ] RCON密码错误,请检查相关设置")
61+
print("[ RCON ] RCON密码错误,请检查相关设置")
62+
time.sleep(2)
63+
subprocess.run(['taskkill', '/f', '/im', self.appName], stderr=subprocess.DEVNULL)
64+
exit(0)
65+
4266
def start_program(self):
4367
INFO.logger.info("[ 启动任务 ] 正在启动程序......")
4468
print("[ 启动任务 ] 正在启动程序......")
4569
program_args = [self.conf['program_path']]
70+
4671
if self.conf['arguments']:
4772
INFO.logger.info("[ 启动任务 ] 已配置额外参数")
4873
print("[ 启动任务 ] 已配置额外参数")
@@ -52,16 +77,30 @@ def start_program(self):
5277
print("[ 启动任务 ] 已开启多核选项")
5378
program_args.extend(["-useperfthreads", "-NoAsyncLoadingThread", "-UseMultithreadForDS"])
5479
print("[ 启动任务 ] 启动参数:", self.conf['arguments'].split())
80+
5581
subprocess.Popen(program_args)
5682

83+
if self.conf['rcon_enabled']:
84+
if self.is_first_run: # 只有在首次运行时才检查RCON连接
85+
INFO.logger.info("[ RCON ] 已开启RCON功能")
86+
print("[ RCON ] 已开启RCON功能")
87+
INFO.logger.info("[ RCON ] 正在检查RCON连接,请等待5秒......")
88+
print("[ RCON ] 正在检查RCON连接,请等待5秒......")
89+
time.sleep(5)
90+
self.check_rcon()
91+
self.is_first_run = False # 首次启动后,将状态标志设置为False
92+
else:
93+
INFO.logger.info("[ RCON ] 未开启RCON功能")
94+
print("[ RCON ] 未开启RCON功能")
95+
5796
# 轮询任务(固定延迟执行)
5897
def polling_task(self):
5998

6099
while True:
61100
if self.conf['restart_interval'] < 60:
62101
INFO.logger.error("[ 轮询任务 ] 服务器重启时间 restart_interval 必须大于等于1分钟,请重新设置!")
63102
print("[ 轮询任务 ] 服务器重启时间 restart_interval 必须大于等于1分钟,请重新设置!")
64-
time.sleep(3)
103+
time.sleep(2)
65104
exit(0)
66105

67106
# 启动程序前检查, 如果存在服务端则不再进行启动操作,改为每次循环结尾关闭进程
@@ -92,7 +131,7 @@ def polling_task(self):
92131
else:
93132
INFO.logger.error("[ 内存监控 ] 轮询间隔 polling_interval_seconds 必须大于等于5秒,请重新设置!")
94133
print("[ 内存监控 ] 轮询间隔 polling_interval_seconds 必须大于5秒,请重新设置!")
95-
time.sleep(3)
134+
time.sleep(2)
96135
exit(0)
97136

98137
print(f'\r[ 轮询任务 ] 服务器将在 {i} 秒后重启......', end='')
@@ -111,8 +150,8 @@ def polling_task(self):
111150
timeout=1) as client:
112151
message = self.conf['shutdown_notices'][str(i)]
113152
response = client.run(f"{self.conf['rcon_command']} {message}", 'utf-8')
114-
INFO.logger.info('Response:{0}'.format(response))
115-
print('Response:', response)
153+
INFO.logger.info('[指令发送] {0}'.format(response))
154+
print('[指令发送] ', response)
116155
except TimeoutError:
117156
INFO.logger.error("RCON连接超时,请检查IP和端口是否填写正确")
118157
print("\r\033[K", end='')

0 commit comments

Comments
 (0)