From 3afec9a63a7b653a366d40382ca0e94f601a9b34 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sat, 24 Aug 2024 13:35:59 +0200 Subject: [PATCH] [+] Safe API closing handling attempt --- app.py | 6 ++++++ firebase_manager.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app.py b/app.py index 0d408c7..d801760 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,7 @@ from flask_cors import CORS import firebase_manager import generic_executor +import atexit app = Flask(__name__) cors = CORS(app, origins="*") @@ -116,7 +117,12 @@ def dynamic_route_handler(path): return generic_response_maker(http.HTTPStatus.BAD_REQUEST, str(e)) +def exit_safety() -> None: + firebase_manager.set_servers_not_running() + return + app.register_blueprint(apiBP) if __name__ == '__main__': + atexit.register(exit_safety) app.run(host='0.0.0.0', port=3000, debug=False) diff --git a/firebase_manager.py b/firebase_manager.py index 110324d..2c076ad 100644 --- a/firebase_manager.py +++ b/firebase_manager.py @@ -154,6 +154,22 @@ def update_server_property(user_id: str, server_name: str, prop: str, value: str server_ref.update({prop: value}) +def set_servers_not_running(): + users_ref = firestore_database.collection(u'users') + docs = users_ref.stream() + + for doc in docs: + user_id = doc.id + servers_ref = firestore_database.collection(u'users').document(user_id).collection(u'servers') + server_docs = servers_ref.stream() + + for server_doc in server_docs: + server_id = server_doc.id + firestore_database.collection(u'users').document(user_id).collection(u'servers').document(server_id).update({u'running': False}) + + print("All servers have been set to not running.") + + 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 = {