Skip to content

Commit

Permalink
🪲 Fix database initialization (hedyorg#5682)
Browse files Browse the repository at this point in the history
QA is down because of a recent PR we merged today: hedyorg#5630 This PR failed to take into account that Heroku uses the function `post_fork` in `gunicorn.conf.py` and not the `if __name__ == '__main__'` located in `app.py`

**How to test**

Everything should work, but the real test is on Alpha.
  • Loading branch information
jpelay authored Jul 31, 2024
1 parent 3f9dd64 commit 799c53e
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
logConfig(LOGGING_CONFIG)
logger = logging.getLogger(__name__)

DATABASE = None
AUTH_MODULE = None
ACHIEVEMENTS_TRANSLATIONS = None
ACHIEVEMENTS = None
SURVEYS = None
STATISTICS = None
FOR_TEACHERS = None

# Todo TB: This can introduce a possible app breaking bug when switching
# to Python 4 -> e.g. Python 4.0.1 is invalid
if (sys.version_info.major < 3 or sys.version_info.minor < 7):
Expand Down Expand Up @@ -2640,7 +2648,27 @@ def on_server_start():
Use this to initialize objects, dependencies and connections.
"""
pass
global DATABASE, AUTH_MODULE, FOR_TEACHERS
DATABASE = database.Database()
AUTH_MODULE = auth_pages.AuthModule(DATABASE)
FOR_TEACHERS = for_teachers.ForTeachersModule(DATABASE, AUTH_MODULE)

app.register_blueprint(auth_pages.AuthModule(DATABASE))
app.register_blueprint(profile.ProfileModule(DATABASE))
app.register_blueprint(programs.ProgramsModule(DATABASE, FOR_TEACHERS))
app.register_blueprint(for_teachers.ForTeachersModule(DATABASE, AUTH_MODULE))
app.register_blueprint(classes.ClassModule(DATABASE))
app.register_blueprint(classes.MiscClassPages(DATABASE))
app.register_blueprint(super_teacher.SuperTeacherModule(DATABASE))
app.register_blueprint(admin.AdminModule(DATABASE))
app.register_blueprint(quiz.QuizModule(DATABASE, QUIZZES))
app.register_blueprint(parsons.ParsonsModule(PARSONS))
app.register_blueprint(statistics.StatisticsModule(DATABASE))
app.register_blueprint(user_activity.UserActivityModule(DATABASE))
app.register_blueprint(tags.TagsModule(DATABASE))
app.register_blueprint(public_adventures.PublicAdventuresModule(DATABASE))
app.register_blueprint(surveys.SurveysModule(DATABASE))
app.register_blueprint(feedback.FeedbackModule(DATABASE))


def try_parse_int(x):
Expand Down Expand Up @@ -2753,32 +2781,11 @@ def on_offline_mode():
no_debug_mode_requested = os.getenv('NO_DEBUG_MODE')
utils.set_debug_mode(not no_debug_mode_requested)

DATABASE = database.Database()
SURVEYS = surveys.SurveysModule(DATABASE)
STATISTICS = statistics.StatisticsModule(DATABASE)
AUTH_MODULE = auth_pages.AuthModule(DATABASE)
FOR_TEACHERS = for_teachers.ForTeachersModule(DATABASE, AUTH_MODULE)

app.register_blueprint(auth_pages.AuthModule(DATABASE))
app.register_blueprint(profile.ProfileModule(DATABASE))
app.register_blueprint(programs.ProgramsModule(DATABASE, FOR_TEACHERS))
app.register_blueprint(for_teachers.ForTeachersModule(DATABASE, AUTH_MODULE))
app.register_blueprint(classes.ClassModule(DATABASE))
app.register_blueprint(classes.MiscClassPages(DATABASE))
app.register_blueprint(super_teacher.SuperTeacherModule(DATABASE))
app.register_blueprint(admin.AdminModule(DATABASE))
app.register_blueprint(quiz.QuizModule(DATABASE, QUIZZES))
app.register_blueprint(parsons.ParsonsModule(PARSONS))
app.register_blueprint(statistics.StatisticsModule(DATABASE))
app.register_blueprint(user_activity.UserActivityModule(DATABASE))
app.register_blueprint(tags.TagsModule(DATABASE))
app.register_blueprint(public_adventures.PublicAdventuresModule(DATABASE))
app.register_blueprint(surveys.SurveysModule(DATABASE))
app.register_blueprint(feedback.FeedbackModule(DATABASE))

if utils.is_offline_mode():
on_offline_mode()

on_server_start()

# Set some default environment variables for development mode
env_defaults = dict(
BASE_URL=f"http://localhost:{config['port']}/",
Expand All @@ -2804,8 +2811,6 @@ def on_offline_mode():

tracemalloc.start()
start_snapshot = tracemalloc.take_snapshot()

on_server_start()
debug = utils.is_debug_mode() and not (is_in_debugger or profile_memory)
if debug:
logger.debug('app starting in debug mode')
Expand Down

0 comments on commit 799c53e

Please sign in to comment.