[+] Safe API closing handling attempt

This commit is contained in:
Charles Le Maux 2024-08-24 13:35:59 +02:00
parent 0d0c655d7f
commit 3afec9a63a
2 changed files with 22 additions and 0 deletions

6
app.py
View File

@ -7,6 +7,7 @@ from flask_cors import CORS
import firebase_manager import firebase_manager
import generic_executor import generic_executor
import atexit
app = Flask(__name__) app = Flask(__name__)
cors = CORS(app, origins="*") cors = CORS(app, origins="*")
@ -116,7 +117,12 @@ def dynamic_route_handler(path):
return generic_response_maker(http.HTTPStatus.BAD_REQUEST, str(e)) 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) app.register_blueprint(apiBP)
if __name__ == '__main__': if __name__ == '__main__':
atexit.register(exit_safety)
app.run(host='0.0.0.0', port=3000, debug=False) app.run(host='0.0.0.0', port=3000, debug=False)

View File

@ -154,6 +154,22 @@ def update_server_property(user_id: str, server_name: str, prop: str, value: str
server_ref.update({prop: value}) 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): 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') new_id: str = datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z%z')
log_entry = { log_entry = {