From a7b48e343755818904f482578bfe02918cc36cf4 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sat, 29 Jun 2024 22:54:47 +0100 Subject: [PATCH] [~] Firestore : added servers running states Signed-off-by: Charles Le Maux --- api_sender.html | 2 +- firebase_manager.py | 6 ++++++ generic_executor.py | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/api_sender.html b/api_sender.html index a4cd19a..a061ce8 100644 --- a/api_sender.html +++ b/api_sender.html @@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => { }); function sendRequest(endpoint, payload) { - return fetch(`https://176.165.62.226:3000/${endpoint}`, { + return fetch(`http://api.servii.fr:3000/${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/firebase_manager.py b/firebase_manager.py index 284ce77..a274167 100644 --- a/firebase_manager.py +++ b/firebase_manager.py @@ -134,6 +134,12 @@ def delete_user(user_id: str): user_ref.delete() +def update_server_running_state(user_id: str, server_name: str, state: bool): + user_ref = firestore_database.collection('users').document(user_id).collection('servers').document(server_name) + if user_ref.get().get('running') != state: + user_ref.set({'running': state}) + + def log_exception_to_firestore(exception: Exception = None, user_id: str = None, data: dict = None): new_id: str = datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z%z') log_entry = { diff --git a/generic_executor.py b/generic_executor.py index 40199ef..f6fae35 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -137,6 +137,10 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: if server_id is None: return HTTPStatus.OK, f"You cannot run multiples instances at this time." mc_manager.servers[server_id]['port'] = int(port) + try: + firebase_manager.update_server_running_state(user_id, name, True) + except Exception as e: + return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error updating server {name}'s running state in database. {type(e).__name__}{str(e)}." return HTTPStatus.ACCEPTED, f"Successfully started server {name}." except Exception as e: print(f"Error when running server: {e}") @@ -145,8 +149,13 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: port = None + user_id = user.uid try: port = firebase_manager.get_server_port(user.uid) + try: + firebase_manager.update_server_running_state(user_id, name, False) + except Exception as e: + pass if port is None: return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore." exists: bool = mc_manager.stop_server(port)