From 82e9831346f4f254e31d895b1a5778fa177a4e54 Mon Sep 17 00:00:00 2001 From: Craig MacFarlane Date: Fri, 19 May 2023 10:39:11 -0700 Subject: [PATCH 1/2] Cleanup site down software, check for site down before throwing NoRoles --- server/app_engine/app_engine.py | 37 +++++++++++++------- server/app_engine/constants.py | 5 +-- server/app_engine/exceptions.py | 11 +++++- server/app_engine/templates/maintenance.html | 27 ++++++++++++++ 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 server/app_engine/templates/maintenance.html diff --git a/server/app_engine/app_engine.py b/server/app_engine/app_engine.py index ea4c59c..bc09649 100644 --- a/server/app_engine/app_engine.py +++ b/server/app_engine/app_engine.py @@ -47,6 +47,8 @@ import dataset_zipper import exceptions from exceptions import NoRoles +from exceptions import DownForMaintenance +from exceptions import ClosedForOffseason import frame_extractor import model_trainer import oidc @@ -432,6 +434,8 @@ def login_via_oidc(): team_roles = oidc.user_getfield('team_roles') + check_site_status() + # # There are a couple reasons that a user might have no team roles, lack # of YPP screening or a global admin or custom role that is not also @@ -532,10 +536,7 @@ def login(): @handle_exceptions @redirect_to_login_if_needed def index(): - if config.config[KEY_SITE_DOWN_FOR_MAINTENANCE]: - return 'This site is temporarily down for maintenance.' - elif config.config[KEY_SITE_CLOSED_FOR_OFFSEASON]: - return flask.render_template('offseason.html') + check_site_status() roles.can_login(flask.session['user_roles'], flask.session['team_number']) @@ -552,10 +553,7 @@ def index(): @handle_exceptions @redirect_to_login_if_needed def label_video(): - if config.config[KEY_SITE_DOWN_FOR_MAINTENANCE]: - return 'This site is temporarily down for maintenance.' - elif config.config[KEY_SITE_CLOSED_FOR_OFFSEASON]: - return flask.render_template('offseason.html') + check_site_status() team_uuid = team_info.retrieve_team_uuid(flask.session, flask.request) data = validate_keys(flask.request.args.to_dict(flat=True), @@ -582,10 +580,7 @@ def label_video(): @handle_exceptions @redirect_to_login_if_needed def monitor_training(): - if config.config[KEY_SITE_DOWN_FOR_MAINTENANCE]: - return 'This site is temporarily down for maintenance.' - elif config.config[KEY_SITE_CLOSED_FOR_OFFSEASON]: - return flask.render_template('offseason.html') + check_site_status() team_uuid = team_info.retrieve_team_uuid(flask.session, flask.request) data = validate_keys(flask.request.args.to_dict(flat=True), @@ -1650,6 +1645,13 @@ def perform_action_gcf(): return 'OK' +def check_site_status(): + if config.config[KEY_SITE_DOWN_FOR_MAINTENANCE]: + raise DownForMaintenance() + elif config.config[KEY_SITE_CLOSED_FOR_OFFSEASON]: + raise ClosedForOffseason() + + # errors def add_userinfo_breadcrumb(): if sentry_dsn is not None: @@ -1708,6 +1710,17 @@ def forbidden_handler(e): error_message="You do not have the required permissions to access this page"), 403 +@app.errorhandler(ClosedForOffseason) +def closed_handler(e): + return flask.render_template('offseason.html') + + +@app.errorhandler(DownForMaintenance) +def maintenance_handler(e): + return flask.render_template('maintenance.html') + + + # For running locally: if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) diff --git a/server/app_engine/constants.py b/server/app_engine/constants.py index b83ca3d..1847da4 100644 --- a/server/app_engine/constants.py +++ b/server/app_engine/constants.py @@ -23,7 +23,8 @@ ORIGIN = os.getenv('ORIGIN') # REDIS_IP_ADDR may be set in the environment in app engine, but not cloud functions. -REDIS_IP_ADDR = os.getenv('REDIS_IP_ADDR') +# REDIS_IP_ADDR = os.getenv('REDIS_IP_ADDR') +REDIS_IP_ADDR = None # Expects to be 'development' or 'production' ENVIRONMENT = os.getenv('ENVIRONMENT') @@ -43,4 +44,4 @@ MAX_VIDEO_RESOLUTION_HEIGHT = 2160 MAX_VIDEOS_TRACKING_PER_TEAM = 3 MAX_BOUNDING_BOX_PER_FRAME = 10 -MAX_DATASETS_PER_TEAM = 20 +MAX_DATASETS_PER_TEAM = 20 \ No newline at end of file diff --git a/server/app_engine/exceptions.py b/server/app_engine/exceptions.py index b57e050..8c97180 100644 --- a/server/app_engine/exceptions.py +++ b/server/app_engine/exceptions.py @@ -54,4 +54,13 @@ def __init__(self, message, status_code=500): class NoRoles(Exception): - pass \ No newline at end of file + pass + + +class DownForMaintenance(Exception): + pass + + +class ClosedForOffseason(Exception): + pass + diff --git a/server/app_engine/templates/maintenance.html b/server/app_engine/templates/maintenance.html new file mode 100644 index 0000000..2e7fd35 --- /dev/null +++ b/server/app_engine/templates/maintenance.html @@ -0,0 +1,27 @@ + +{% extends 'layout.html' %} + +{% block content %} +
+
+

The Machine Learning Toolchain is currently down for maintenance.

+
+
+ +
+
+{% endblock %} From 181bbf5bb53bd1fbfa0d6a982cc5b1af084a07d6 Mon Sep 17 00:00:00 2001 From: Craig MacFarlane Date: Fri, 19 May 2023 10:47:53 -0700 Subject: [PATCH 2/2] Revert unintended change --- server/app_engine/constants.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/app_engine/constants.py b/server/app_engine/constants.py index 1847da4..b83ca3d 100644 --- a/server/app_engine/constants.py +++ b/server/app_engine/constants.py @@ -23,8 +23,7 @@ ORIGIN = os.getenv('ORIGIN') # REDIS_IP_ADDR may be set in the environment in app engine, but not cloud functions. -# REDIS_IP_ADDR = os.getenv('REDIS_IP_ADDR') -REDIS_IP_ADDR = None +REDIS_IP_ADDR = os.getenv('REDIS_IP_ADDR') # Expects to be 'development' or 'production' ENVIRONMENT = os.getenv('ENVIRONMENT') @@ -44,4 +43,4 @@ MAX_VIDEO_RESOLUTION_HEIGHT = 2160 MAX_VIDEOS_TRACKING_PER_TEAM = 3 MAX_BOUNDING_BOX_PER_FRAME = 10 -MAX_DATASETS_PER_TEAM = 20 \ No newline at end of file +MAX_DATASETS_PER_TEAM = 20