MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / run_status

Method run_status

mod/project/java/groupMod.py:237–325  ·  view source on GitHub ↗
(self, last_write_time: float)

Source from the content-addressed store, hash-verified

235
236 # 获取运行状态信息
237 def run_status(self, last_write_time: float) -> dict:
238 default_error = {
239 "running": False,
240 "msg": "操作出错,已退出",
241 "running_data": None,
242 "last_write_time": 0,
243 }
244 pid_file = "{}/{}.pid".format(Group.GROUP_TMP_DIR, self.group_id)
245 log_file = "{}/{}.log".format(Group.GROUP_TMP_DIR, self.group_id)
246 if not os.path.isfile(pid_file):
247 return default_error
248 try:
249 pid = int(public.readFile(pid_file))
250 except:
251 return default_error
252
253 try:
254 data = json.loads(public.readFile(log_file))
255 except: # 如果读取出错,则说明进程出现问题,则杀死进程,并清除数据,返回错误
256 if os.path.isfile(log_file):
257 os.remove(log_file)
258 if os.path.isfile(pid_file):
259 os.remove(pid_file)
260 if self.is_running(pid):
261 psutil.Process(pid).kill()
262
263 return default_error
264
265 # 如果进程不在运行,则返回上一次运行的数据(防止卡顿导致导致最后的数据没有读取到)
266 if not self.is_running(pid):
267 return {
268 "running": False,
269 "msg": "操作进程已退出",
270 "running_data": data,
271 "last_write_time": os.path.getmtime(log_file),
272 }
273
274 # 如果还在运行, 则进行长链接, 等待最多5秒, 如果没有数据被写入, 且进程依旧在运行,则返回上一次的数据;
275 # 若不在运行则说明,且没有写入则说明等待期间出错了 (启动进程退出前必定会进行一次写入)
276 # 如果有数据被写入则返回最新数据
277
278 for i in range(50):
279 m_time = os.path.getmtime(log_file)
280 if abs(m_time - last_write_time) > 0.001: # 如果时间差大于0.001秒,则说明数据再次被写入了,则退出循环,返回数据
281 now_write_time = m_time
282 break
283 elif i == 49: # 最后一次检测后,直接退出循环
284 continue
285 else:
286 time.sleep(0.1)
287 else: # 如果 5 秒内没有数据被写入,则返回上一次的数据
288 if not self.is_running(pid): # 不在运行, 说明等待期间出错了
289 if os.path.isfile(log_file):
290 os.remove(log_file)
291 if os.path.isfile(pid_file):
292 os.remove(pid_file)
293 return default_error
294

Callers 1

get_run_statusMethod · 0.95

Calls 6

is_runningMethod · 0.95
readFileMethod · 0.80
killMethod · 0.80
formatMethod · 0.45
loadsMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected