Don't create new app instance for background tasks. Reuse the old one!

This commit is contained in:
Patrick Jentsch 2019-09-02 10:42:28 +02:00
parent 565d273dd4
commit e658d2bbd0
2 changed files with 5 additions and 14 deletions

View File

@ -13,16 +13,12 @@ mail = Mail()
socketio = SocketIO() socketio = SocketIO()
def create_app(config_name, main=True): def create_app(config_name):
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(config[config_name]) app.config.from_object(config[config_name])
config[config_name].init_app(app) config[config_name].init_app(app)
db.init_app(app) db.init_app(app)
if not main:
return app
login_manager.init_app(app) login_manager.init_app(app)
mail.init_app(app) mail.init_app(app)
socketio.init_app(app, message_qeue='redis://') socketio.init_app(app, message_qeue='redis://')

View File

@ -1,6 +1,6 @@
from flask import request from flask import current_app, request
from flask_login import current_user, login_required from flask_login import current_user, login_required
from .. import create_app, db, socketio from .. import db, socketio
from ..models import User from ..models import User
import json import json
import jsonpatch import jsonpatch
@ -23,6 +23,7 @@ def connect():
' task associated with the sid. ' task associated with the sid.
''' '''
socketio.start_background_task(background_task, socketio.start_background_task(background_task,
current_app._get_current_object(),
current_user.id, current_user.id,
request.sid) request.sid)
@ -37,7 +38,7 @@ def disconnect():
disconnected.append(request.sid) 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 ' 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 ' 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' ' > 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(): with app.app_context():
user = db.session.query(User).filter_by(id=user_id).first() user = db.session.query(User).filter_by(id=user_id).first()
''' Get current values from the database. ''' ''' Get current values from the database. '''