[+] Server deletion safety

Errors are correctly logged in firebase.
User can't break the website anymore.

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-07-11 16:12:16 +01:00
parent 87a242f131
commit fa9ba6d3e0
2 changed files with 10 additions and 5 deletions

View File

@ -107,6 +107,11 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p
def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]: def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]:
user_id = user.uid user_id = user.uid
try:
port: int = firebase_manager.get_server_port(user_id)
mc_manager.stop_server_forcefully(port)
except Exception as e:
file_manager.log_error(type(e).__name__, str(e)+f" error when stopping server {name}")
server_path: str = f"users/{user_id}/{name}" server_path: str = f"users/{user_id}/{name}"
try: try:
firebase_manager.delete_server(user_id, name) firebase_manager.delete_server(user_id, name)

View File

@ -18,7 +18,7 @@ class MinecraftServerManager:
if port in self.servers: if port in self.servers:
return None return None
command = f"{java_executable} -Xmx{memory_size} -Xms{memory_size} -jar {jar_file} > /dev/null" command = f"{java_executable} -Xmx{memory_size} -Xms{memory_size} -jar {jar_file} --nogui > /dev/null"
process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE) process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE)
self.servers_count = len(self.servers) + 1 self.servers_count = len(self.servers) + 1
@ -45,12 +45,12 @@ class MinecraftServerManager:
del self.servers[port] del self.servers[port]
return True return True
def stop_server_forcefully(self, server_id) -> bool: def stop_server_forcefully(self, port) -> bool:
if server_id not in self.servers: if port not in self.servers:
return False return False
process = self.servers[server_id]['process'] process = self.servers[port]['process']
process.terminate() process.terminate()
del self.servers[server_id] del self.servers[port]
return True return True
def get_servers(self): def get_servers(self):