Skip to content
Snippets Groups Projects
Commit f2fb2b64 authored by Arie Peterson's avatar Arie Peterson
Browse files

Do not run initialization code concurrently

parent 1378cee3
No related branches found
No related tags found
1 merge request!86Draft: Resolve "Make it easier to add an app"
Pipeline #36589 passed with stages
in 5 minutes and 36 seconds
...@@ -3,6 +3,7 @@ from flask_cors import CORS ...@@ -3,6 +3,7 @@ from flask_cors import CORS
from flask_jwt_extended import JWTManager from flask_jwt_extended import JWTManager
import flask_migrate import flask_migrate
from jsonschema.exceptions import ValidationError from jsonschema.exceptions import ValidationError
from NamedAtomicLock import NamedAtomicLock
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest
# These imports are required # These imports are required
...@@ -68,27 +69,36 @@ app.logger.info("Starting dashboard backend.") ...@@ -68,27 +69,36 @@ app.logger.info("Starting dashboard backend.")
cors = CORS(app) cors = CORS(app)
db.init_app(app) db.init_app(app)
with app.app_context():
# We have reset the alembic migration history at Stackspin version 2.2. # We'll now perform some initialization routines. Because these have to be done
# This checks whether we need to prepare the database to follow that # once at startup, not for every gunicorn worker, we take a machine-wide lock
# change. # for this.
migration_reset.reset() init_lock = NamedAtomicLock('dashboard_init')
flask_migrate.Migrate(app, db) if init_lock.acquire():
try: try:
with app.app_context(): with app.app_context():
flask_migrate.upgrade() # We have reset the alembic migration history at Stackspin version 2.2.
except Exception as e: # This checks whether we need to prepare the database to follow that
app.logger.info(f"upgrade failed: {type(e)}: {e}") # change.
sys.exit(2) migration_reset.reset()
flask_migrate.Migrate(app, db)
# We need this app context in order to talk the database, which is managed by try:
# flask-sqlalchemy, which assumes a flask app context. with app.app_context():
with app.app_context(): flask_migrate.upgrade()
# Load the list of apps from a configmap and store any missing ones in the except Exception as e:
# database. app.logger.info(f"upgrade failed: {type(e)}: {e}")
cluster_config.populate_apps() sys.exit(2)
# Same for the list of oauthclients.
cluster_config.populate_oauthclients() # We need this app context in order to talk the database, which is managed by
# flask-sqlalchemy, which assumes a flask app context.
with app.app_context():
# Load the list of apps from a configmap and store any missing ones in the
# database.
cluster_config.populate_apps()
# Same for the list of oauthclients.
cluster_config.populate_oauthclients()
finally:
init_lock.release()
app.register_blueprint(api_v1) app.register_blueprint(api_v1)
app.register_blueprint(web) app.register_blueprint(web)
......
...@@ -19,6 +19,7 @@ jinja2-base64-filters==0.1.4 ...@@ -19,6 +19,7 @@ jinja2-base64-filters==0.1.4
kubernetes==24.2.0 kubernetes==24.2.0
MarkupSafe==2.1.1 MarkupSafe==2.1.1
mypy-extensions==0.4.3 mypy-extensions==0.4.3
NamedAtomicLock==1.1.3
oauthlib==3.2.0 oauthlib==3.2.0
pathspec==0.9.0 pathspec==0.9.0
platformdirs==2.5.1 platformdirs==2.5.1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment