Make the app arg in @background functions a bit less magical

This commit is contained in:
Patrick Jentsch 2020-04-23 08:35:18 +02:00
parent 83c05d93b0
commit 3a989534cf
4 changed files with 12 additions and 10 deletions

View File

@ -7,7 +7,8 @@ import shutil
@background
def build_corpus(app, corpus_id):
def build_corpus(corpus_id, *args, **kwargs):
app = kwargs['app']
with app.app_context():
corpus = Corpus.query.get(corpus_id)
if corpus is None:
@ -48,7 +49,8 @@ def build_corpus(app, corpus_id):
@background
def delete_corpus(app, corpus_id):
def delete_corpus(corpus_id, *args, **kwargs):
app = kwargs['app']
with app.app_context():
corpus = Corpus.query.get(corpus_id)
if corpus is None:
@ -60,7 +62,8 @@ def delete_corpus(app, corpus_id):
@background
def delete_corpus_file(app, corpus_file_id):
def delete_corpus_file(corpus_file_id, *args, **kwargs):
app = kwargs['app']
with app.app_context():
corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None:

View File

@ -19,8 +19,8 @@ def background(f):
''' This decorator executes a function in a Thread '''
@wraps(f)
def wrapped(*args, **kwargs):
app = current_app._get_current_object()
thread = Thread(target=f, args=(app, *args), kwargs=kwargs)
kwargs['app'] = current_app._get_current_object()
thread = Thread(target=f, args=args, kwargs=kwargs)
thread.start()
return thread
return wrapped

View File

@ -7,7 +7,8 @@ import shutil
@background
def delete_job(app, job_id):
def delete_job(job_id, *args, **kwargs):
app = kwargs['app']
with app.app_context():
job = Job.query.get(job_id)
if job is None:

View File

@ -1,4 +1,3 @@
from .. import logger
from ..decorators import background
from ..models import User
import os
@ -6,13 +5,12 @@ import shutil
@background
def delete_user(app, user_id):
logger.warning('aufgerufen')
def delete_user(user_id, *args, **kwargs):
app = kwargs['app']
with app.app_context():
user = User.query.get(user_id)
if user is None:
raise Exception('User {} not found!'.format(user_id))
logger.warning('deleting user')
path = os.path.join(app.config['NOPAQUE_STORAGE'], str(user.id))
shutil.rmtree(path, ignore_errors=True)
user.delete()