Fixed orphaned processes and added logging

This commit is contained in:
Malasaur 2025-12-16 23:57:35 +01:00
parent 17fc911290
commit cd2eb8de9d
No known key found for this signature in database
6 changed files with 177 additions and 46 deletions

34
util.py
View file

@ -1,14 +1,21 @@
from asyncio import sleep
from time import sleep
from typing import Callable
from classes import ProcessStatus
from config import Config
from controllers import Controllers
from fastapi import HTTPException
from logs import logger
async def stop_server(
action: str, countdown: int, reason: str, timeout: int, then: Callable | None = None
def stop_server(
action: str,
countdown: int,
reason: str,
timeout: int,
then: Callable | None = None,
*args,
**kwargs,
):
"""Warns the players, stops the Server, and kills its process if its taking too long.
@ -19,27 +26,42 @@ async def stop_server(
timeout (int): Seconds to wait before killing the process.
then (Callable | None) (default: None): Function to be called after the Server is stopped.
"""
logger.debug(
'stop_server(action="%s", countdown=%s, reason="%s", timeout=%s, then=%s, args=%s, kwargs=%s',
action,
countdown,
reason,
timeout,
then,
args,
kwargs,
)
if countdown:
logger.debug("stop_server(...) - Starting countdown")
Controllers.server.command(f"say SERVER IS {action} IN {countdown} SECONDS!!!")
Controllers.server.command(f"say REASON: '{reason}'")
while countdown > 0 and Controllers.server.status().get("players", {}).get(
"online", 0
):
await sleep(1)
sleep(1)
countdown -= 1
logger.info("stop_server(...) - Stopping server")
Controllers.server.command("stop")
while timeout > 0 and Controllers.process.status() == ProcessStatus.RUNNING:
await sleep(1)
sleep(1)
timeout -= 1
if Controllers.process.status() == ProcessStatus.RUNNING:
logger.warning("stop_server(...) - Killing server")
Controllers.process.kill()
if then:
then()
logger.debug("stop_server(...) - Running callback")
then(*args, **kwargs)
def check_password(password: str):
if password != Config.MINECRAFTD_PASSWORD:
logger.debug('check_password("%s") - Password is invalid', password)
raise HTTPException(401, "Password is invalid")