@@ -41,27 +41,28 @@ def patched_run(self, command: str, *args: str, encoding: str = "utf-8") -> str:
41
41
Client .run = patched_run
42
42
43
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 )
44
+ while True :
45
+ try :
46
+ with Client (
47
+ host = self .conf ['rcon_host' ],
48
+ port = self .conf ['rcon_port' ],
49
+ passwd = self .conf ['rcon_password' ],
50
+ timeout = 1 ):
51
+ INFO .logger .info ("[ RCON ] RCON连接正常" )
52
+ print ("\r [ RCON ] RCON连接正常\n " , end = '' , flush = True )
53
+ time .sleep (1 )
54
+ return True
55
+
56
+ except TimeoutError :
57
+ INFO .logger .error ("[ RCON ] 正在检测RCON连接,请不要关闭......" )
58
+ print ("[ RCON ] 正在检测RCON连接,请不要关闭......" )
59
+ return False
60
+ except rcon .exceptions .WrongPassword :
61
+ INFO .logger .error ("[ RCON ] RCON密码错误,请检查相关设置" )
62
+ print ("[ RCON ] RCON密码错误,请检查相关设置" )
63
+ time .sleep (2 )
64
+ subprocess .run (['taskkill' , '/f' , '/im' , self .appName ], stderr = subprocess .DEVNULL )
65
+ exit (0 )
65
66
66
67
def start_program (self ):
67
68
INFO .logger .info ("[ 启动任务 ] 正在启动程序......" )
@@ -80,15 +81,27 @@ def start_program(self):
80
81
81
82
subprocess .Popen (program_args )
82
83
84
+ # 尝试连接
83
85
if self .conf ['rcon_enabled' ]:
84
86
if self .is_first_run : # 只有在首次运行时才检查RCON连接
85
87
INFO .logger .info ("[ RCON ] 已开启RCON功能" )
86
88
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
89
+ INFO .logger .info ("[ RCON ] 正在检查RCON连接,请等待最多15秒......" )
90
+ print ("[ RCON ] 正在检查RCON连接,请等待最多15秒......" )
91
+
92
+ start_time = time .time ()
93
+ while time .time () - start_time < 15 :
94
+ if self .check_rcon ():
95
+ break
96
+ time .sleep (1 ) # 每次尝试后,暂停1秒
97
+
98
+ if time .time () - start_time >= 15 :
99
+ INFO .logger .error ("[ RCON ] 无法在15秒内建立RCON连接" )
100
+ print ("[ RCON ] 无法在15秒内建立RCON连接" )
101
+ exit (0 )
102
+
103
+ self .is_first_run = False
104
+
92
105
else :
93
106
INFO .logger .info ("[ RCON ] 未开启RCON功能" )
94
107
print ("[ RCON ] 未开启RCON功能" )
@@ -111,11 +124,13 @@ def polling_task(self):
111
124
self .start_program ()
112
125
113
126
INFO .logger .info (f'[ 轮询任务 ] 服务器将进入重启倒计时,设置时长为 { self .conf ["restart_interval" ]} 秒......' )
114
- print (f'[ 轮询任务 ] 服务器将进入重启倒计时,设置时长为 { self .conf ["restart_interval" ]} 秒......' )
127
+ print (f'\r [ 轮询任务 ] 服务器将进入重启倒计时,设置时长为 { self .conf ["restart_interval" ]} 秒......' )
115
128
116
129
# 如果设置了检查内存使用情况memory_monitor_enabled
117
130
if self .conf ['memory_monitor_enabled' ]:
118
- print ("[ 内存监控 ] 已开启内存监控,将在内存使用超过阈值时重启程序......" )
131
+ time .sleep (1 )
132
+ INFO .logger .info (f"[ 内存监控 ] 已开启内存监控,每{ self .conf ['polling_interval_seconds' ]} 秒检查一次,将在内存使用超过{ self .conf ['memory_usage_threshold' ]} %时重启程序" )
133
+ print (f"\r [ 内存监控 ] 已开启内存监控,每{ self .conf ['polling_interval_seconds' ]} 秒检查一次,将在内存使用超过{ self .conf ['memory_usage_threshold' ]} %时重启程序" )
119
134
120
135
# 服务器持续运行时间(重启间隔)
121
136
for i in range (int (self .conf ['restart_interval' ]), 0 , - 1 ):
@@ -126,6 +141,7 @@ def polling_task(self):
126
141
mem_usage = mem_info .percent # 获取内存使用百分比
127
142
# 如果内存使用超过阈值,则跳出倒计时,进行重启操作
128
143
if mem_usage > self .conf ['memory_usage_threshold' ]:
144
+ INFO .logger .error (f"[ 内存监控 ] 内存使用超过{ self .conf ['memory_usage_threshold' ]} %,正在重启程序......" )
129
145
print (f"[ 内存监控 ] 内存使用超过{ self .conf ['memory_usage_threshold' ]} %,正在重启程序......" )
130
146
break
131
147
else :
@@ -134,8 +150,6 @@ def polling_task(self):
134
150
time .sleep (2 )
135
151
exit (0 )
136
152
137
- print (f'\r [ 轮询任务 ] 服务器将在 { i } 秒后重启......' , end = '' )
138
-
139
153
# 还剩30秒的时候发送rcon关服消息提醒
140
154
if str (i ) in self .conf ['shutdown_notices' ] and self .conf ['rcon_enabled' ]: # 检查是否有对应的通知
141
155
if self .conf ['rcon_command' ]:
@@ -174,10 +188,9 @@ def polling_task(self):
174
188
self .start_program ()
175
189
176
190
def start_daemon (self ):
177
- # 守护进程代码
191
+ # 守护进程
178
192
while True :
179
193
try :
180
- # 检查服务是否在运行
181
194
result = subprocess .run (['tasklist' , '/FI' , 'IMAGENAME eq PalServer.exe' ], capture_output = True ,
182
195
text = True )
183
196
if 'PalServer.exe' not in result .stdout :
@@ -187,13 +200,10 @@ def start_daemon(self):
187
200
# 启动程序
188
201
self .start_program ()
189
202
190
- else :
191
- print ("\r \033 [K" , end = '' )
192
- print ('[ 守护进程 ] 监控到 PalServer 正在运行中,无需任何操作' )
193
- # 倒数计时
203
+ # 倒计时
194
204
for i in range (int (self .conf ['daemon_time' ]), 0 , - 1 ):
195
- print ("[ 守护进程 ] 将在 {0} 秒后进行下次检测" .format (i ), end = '' )
196
205
time .sleep (1 )
206
+
197
207
# 只有异常退出才会触发,手动关闭进程不会触发
198
208
except Exception as e :
199
209
INFO .logger .error (f"[ 守护进程 ] 程序异常终止,错误信息:{ e } \n 正在尝试重启程序......" )
@@ -213,11 +223,9 @@ def main():
213
223
# [ 轮询任务 ] 必须在最初启动 防止[ 轮询任务 ] kill掉[ 守护进程 ] 刚启动的服务端
214
224
if Task .conf ['daemon_enabled' ]:
215
225
print ("\r \033 [K" , end = '' )
216
- print ("[ 守护进程 ] 已配置,在 5 秒后监控 PalServer 进程" )
226
+ INFO .logger .info ("[ 守护进程 ] 守护进程已开启,延迟5秒启动避免双端开启,每隔{0}秒检查一次......" .format (Task .conf ['daemon_time' ]))
227
+ print ("[ 守护进程 ] 守护进程已开启,延迟5秒启动避免双端开启,每隔{0}秒检查一次......" .format (Task .conf ['daemon_time' ]))
217
228
time .sleep (5 ) # 再延迟5秒 避免脚本启动时双开服务端。尽量避免10结尾以免和[ 轮询任务 ] 倒计时同时结束
218
- INFO .logger .info ("[ 守护进程 ] 已启动,每隔{0}秒检测 PalServer 进程......" .format (Task .conf ['daemon_time' ]))
219
- print ("\r \033 [K" , end = '' )
220
- print ("[ 守护进程 ] 已启动,每隔{0}秒检测 PalServer 进程......" .format (Task .conf ['daemon_time' ]))
221
229
daemon_thread = threading .Thread (target = Task .start_daemon )
222
230
daemon_thread .start ()
223
231
0 commit comments