mirror of
https://github.com/0O0o0oOoO00/Alas.git
synced 2026-05-14 07:49:25 +08:00
add: check alas prev status
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -282,3 +282,5 @@ blcrack/cracker/webui/.idea/
|
||||
|
||||
3rdparty/build/
|
||||
3rdparty/pkgs/
|
||||
|
||||
bin/status/
|
||||
@@ -1,4 +1,5 @@
|
||||
import datetime
|
||||
import pathlib
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
@@ -7,6 +8,7 @@ from typing import Dict
|
||||
from module.config.config import AzurLaneConfig
|
||||
from module.counter import MaxCounter
|
||||
from module.notify import handle_notify
|
||||
from module.submodule.utils import has_config_mod
|
||||
from module.webui.process_manager import ProcessManager
|
||||
|
||||
|
||||
@@ -18,6 +20,7 @@ class InstanceSetting:
|
||||
|
||||
|
||||
class InstanceWatcher:
|
||||
STATUS_DIR = "./bin/status"
|
||||
instance = None
|
||||
|
||||
def __init__(self):
|
||||
@@ -66,6 +69,28 @@ class InstanceWatcher:
|
||||
for i in ins_has_triggered:
|
||||
self.remove_instance(i)
|
||||
|
||||
def add_status_file(self, name):
|
||||
f = pathlib.Path(InstanceWatcher.STATUS_DIR) / f"{name}.status"
|
||||
if not f.exists():
|
||||
f.parent.mkdir(parents=True, exist_ok=True)
|
||||
f.touch()
|
||||
|
||||
def remove_status_file(self, name):
|
||||
f = pathlib.Path(InstanceWatcher.STATUS_DIR) / f"{name}.status"
|
||||
if f.exists():
|
||||
f.parent.mkdir(parents=True, exist_ok=True)
|
||||
f.unlink()
|
||||
|
||||
def get_all_prev_instance(self):
|
||||
f = pathlib.Path(InstanceWatcher.STATUS_DIR)
|
||||
if not f.exists():
|
||||
f.mkdir(parents=True, exist_ok=True)
|
||||
return [i.stem for i in f.glob("*.status")]
|
||||
|
||||
def has_status_file(self, name):
|
||||
f = pathlib.Path(InstanceWatcher.STATUS_DIR) / f"{name}.status"
|
||||
return f.exists()
|
||||
|
||||
def try_add_instance(self, name):
|
||||
if name in self.instances:
|
||||
return
|
||||
@@ -78,10 +103,22 @@ class InstanceWatcher:
|
||||
notify_config=full_config.Alas_Error_OnePushConfig
|
||||
)
|
||||
self.instances[name] = setting
|
||||
self.add_status_file(name)
|
||||
|
||||
def check_instance_status(self):
|
||||
for config_name in self.get_all_prev_instance():
|
||||
if not has_config_mod(config_name):
|
||||
continue
|
||||
ins = ProcessManager.get_manager(config_name)
|
||||
if ins.alive:
|
||||
continue
|
||||
if ins.detailed_instance_status == ProcessManager.DetailInstanceStatus.NoRenderables:
|
||||
ins.start("alas")
|
||||
|
||||
def remove_instance(self, name):
|
||||
try:
|
||||
self.instances.pop(name)
|
||||
self.remove_status_file(name)
|
||||
except Exception:
|
||||
...
|
||||
|
||||
@@ -91,6 +128,7 @@ class InstanceWatcher:
|
||||
try:
|
||||
self.check_counter()
|
||||
self.check_instances()
|
||||
self.check_instance_status()
|
||||
except Exception:
|
||||
...
|
||||
|
||||
|
||||
@@ -86,3 +86,6 @@ def get_config_mod(config_name):
|
||||
return MOD_CONFIG_DICT[config_name]
|
||||
except KeyError:
|
||||
return 'alas'
|
||||
|
||||
def has_config_mod(config_name):
|
||||
return os.path.exists(f"./config/{config_name}.json")
|
||||
@@ -237,6 +237,14 @@ from module.instance_watcher import InstanceWatcher
|
||||
|
||||
|
||||
class ProcessManager(ProcessManager):
|
||||
class DetailInstanceStatus:
|
||||
Alive = 1
|
||||
NoRenderables = 2
|
||||
ManualStop = 3
|
||||
Finish = 4
|
||||
Update = 5
|
||||
Error = 6
|
||||
|
||||
def start(self, *args, **kwargs):
|
||||
super().start(*args, **kwargs)
|
||||
InstanceWatcher.get_instance().try_add_instance(self.config_name)
|
||||
@@ -244,3 +252,23 @@ class ProcessManager(ProcessManager):
|
||||
def stop(self, *args, **kwargs):
|
||||
InstanceWatcher.get_instance().remove_instance(self.config_name)
|
||||
super().stop(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def detailed_instance_status(self) -> int:
|
||||
if self.alive:
|
||||
return ProcessManager.DetailInstanceStatus.Alive
|
||||
elif len(self.renderables) == 0:
|
||||
return ProcessManager.DetailInstanceStatus.NoRenderables
|
||||
else:
|
||||
console = Console(no_color=True)
|
||||
with console.capture() as capture:
|
||||
console.print(self.renderables[-1])
|
||||
s = capture.get().strip()
|
||||
if s.endswith("Reason: Manual stop"):
|
||||
return ProcessManager.DetailInstanceStatus.ManualStop
|
||||
elif s.endswith("Reason: Finish"):
|
||||
return ProcessManager.DetailInstanceStatus.Finish
|
||||
elif s.endswith("Reason: Update"):
|
||||
return ProcessManager.DetailInstanceStatus.Update
|
||||
else:
|
||||
return ProcessManager.DetailInstanceStatus.Error
|
||||
|
||||
Reference in New Issue
Block a user