From e658d2bbd0ce461cb2571874b8dffacf3897d56f Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Mon, 2 Sep 2019 10:42:28 +0200
Subject: [PATCH] Don't create new app instance for background tasks. Reuse the
old one!
---
app/__init__.py | 6 +-----
app/main/events.py | 13 ++++---------
2 files changed, 5 insertions(+), 14 deletions(-)
diff --git a/app/__init__.py b/app/__init__.py
index 19bfa517..bd42c79c 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -13,16 +13,12 @@ mail = Mail()
socketio = SocketIO()
-def create_app(config_name, main=True):
+def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
db.init_app(app)
-
- if not main:
- return app
-
login_manager.init_app(app)
mail.init_app(app)
socketio.init_app(app, message_qeue='redis://')
diff --git a/app/main/events.py b/app/main/events.py
index b0f2ce04..89a3d7cd 100644
--- a/app/main/events.py
+++ b/app/main/events.py
@@ -1,6 +1,6 @@
-from flask import request
+from flask import current_app, request
from flask_login import current_user, login_required
-from .. import create_app, db, socketio
+from .. import db, socketio
from ..models import User
import json
import jsonpatch
@@ -23,6 +23,7 @@ def connect():
' task associated with the sid.
'''
socketio.start_background_task(background_task,
+ current_app._get_current_object(),
current_user.id,
request.sid)
@@ -37,7 +38,7 @@ def disconnect():
disconnected.append(request.sid)
-def background_task(user_id, session_id):
+def background_task(app, user_id, session_id):
'''
' Sends initial corpus and job lists to the client. Afterwards it checks
' every 3 seconds if changes to the initial values appeared. If changes are
@@ -48,12 +49,6 @@ def background_task(user_id, session_id):
'
' > where '*' is either 'corpora' or 'jobs'
'''
- '''
- ' Create an app instance to get access to an app_context with which db
- ' operations are fulfilled.
- '''
- app = create_app(os.getenv('FLASK_CONFIG') or 'default', main=False)
-
with app.app_context():
user = db.session.query(User).filter_by(id=user_id).first()
''' Get current values from the database. '''