Changed ProcessController to handle Server as child subprocess

This commit is contained in:
Malasaur 2025-12-01 22:43:15 +01:00
parent 9742cafad9
commit a008bc27fa
No known key found for this signature in database
5 changed files with 52 additions and 53 deletions

View file

@ -1,70 +1,56 @@
import shlex
from collections import deque
from os import setsid
from pathlib import Path
from subprocess import PIPE, Popen
from time import sleep
from typing import Generator
from typing import Generator, Literal
from mcrcon import MCRcon
from mcstatus import JavaServer
from psutil import NoSuchProcess, Process
from .classes import ServerStatus
from .classes import ProcessStatus, ServerStatus
from .config import Config
class ProcessController:
def __init__(self):
self.pid_file: Path = Path(Config.PID_FILE)
self.start_command: list[str] = shlex.split(Config.START_COMMAND)
self.process: Process | None = None
if self.pid_file.is_file():
pid = int(self.pid_file.read_text())
if self._is_pid_alive(pid):
self.process = Process(pid)
else:
self.pid_file.unlink()
self.process: Popen | None = None
self.last_status: Literal[ProcessStatus.STOPPED, ProcessStatus.CRASHED] = (
ProcessStatus.STOPPED
)
def start(self) -> None:
"Starts the process."
if self.is_started():
"Start the process."
if self.status() == ProcessStatus.RUNNING:
return
process = Popen(
self.process = Popen(
self.start_command,
stdout=PIPE,
stderr=PIPE,
preexec_fn=setsid,
)
self.process = Process(process.pid)
self.pid_file.write_text(str(self.process.pid))
def is_started(self) -> bool:
"Check if the process is running."
if self.process:
return self.process.is_running()
return False
def is_crashed(self) -> bool:
"Check if the process has crashed."
return False # TODO
def status(self) -> ProcessStatus:
"Check the process' status."
if not self.process:
return self.last_status
match self.process.poll():
case None:
return ProcessStatus.RUNNING
case 0:
self.last_status = ProcessStatus.STOPPED
return ProcessStatus.STOPPED
case _:
self.last_status = ProcessStatus.CRASHED
return ProcessStatus.CRASHED
def kill(self) -> None:
"Kill the process."
if self.process:
self.process.terminate()
self.process = None
self.pid_file.unlink()
def _is_pid_alive(self, pid: int) -> bool:
"Check if a process with the given PID is alive."
try:
p = Process(pid)
return p.is_running()
except NoSuchProcess:
return False
self.last_status = ProcessStatus.STOPPED
class ServerController:
@ -102,10 +88,13 @@ class ServerController:
}
def command(self, command: str) -> str:
self.rcon.connect()
output = self.rcon.command(command)
self.rcon.disconnect()
return output
try:
self.rcon.connect()
output = self.rcon.command(command)
self.rcon.disconnect()
return output
except Exception:
return ""
class MaintainanceController: