From 958779ec61feca68fba48ec2bed6408a80fd7571 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 6 Nov 2019 10:07:34 +0100 Subject: [PATCH] Use background tasks for file operations. --- app/corpora/background_tasks.py | 25 ++++++++++++++ app/corpora/views.py | 59 ++++++++++++++++++--------------- app/utils.py | 15 --------- 3 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 app/corpora/background_tasks.py diff --git a/app/corpora/background_tasks.py b/app/corpora/background_tasks.py new file mode 100644 index 00000000..e12cf51c --- /dev/null +++ b/app/corpora/background_tasks.py @@ -0,0 +1,25 @@ +from app.models import Corpus, CorpusFile + + +def delete_corpus_(app, corpus_id): + with app.app_context(): + corpus = Corpus.query.filter_by(id=corpus_id).first() + if corpus is None: + raise Exception('Corpus {} not found!'.format(corpus_id)) + corpus.delete() + + +def delete_corpus_file_(app, corpus_file_id): + with app.app_context(): + corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first() + if corpus_file is None: + raise Exception('Corpus file {} not found!'.format(corpus_file_id)) + corpus_file.delete() + + +def edit_corpus_file_(app, corpus_file_id): + with app.app_context(): + corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first() + if corpus_file is None: + raise Exception('Corpus file {} not found!'.format(corpus_file_id)) + corpus_file.insert_metadata() diff --git a/app/corpora/views.py b/app/corpora/views.py index 9f95fbbc..096e8957 100644 --- a/app/corpora/views.py +++ b/app/corpora/views.py @@ -1,11 +1,12 @@ from app import db from app.models import Corpus, CorpusFile -from app.utils import background_prepare_corpus_file from flask import (abort, current_app, flash, redirect, request, render_template, url_for, send_from_directory) from flask_login import current_user, login_required from werkzeug.utils import secure_filename from . import corpora +from .background_tasks import (delete_corpus_, delete_corpus_file_, + edit_corpus_file_) from .forms import (AddCorpusFileForm, AddCorpusForm, EditCorpusFileForm, QueryDownloadForm, QueryForm) import os @@ -80,29 +81,16 @@ def corpus_analysis(corpus_id): title='Corpus: {}'.format(corpus.title)) -@corpora.route('//prepare') -@login_required -def prepare_corpus(corpus_id): - corpus = Corpus.query.get_or_404(corpus_id) - if not (corpus.creator == current_user or current_user.is_administrator()): - abort(403) - if len(corpus.files.all()) > 0: - corpus.status = 'preparable' - db.session.commit() - flash('Corpus marked for preparation!') - else: - flash('Can not prepare corpus, please add corpus file(s).') - return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) - - @corpora.route('//delete') @login_required def delete_corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id) if not (corpus.creator == current_user or current_user.is_administrator()): abort(403) - delete_thread = threading.Thread(corpus.delete()) - delete_thread.start() + thread = threading.Thread(target=delete_corpus_, + args=(current_app._get_current_object(), + corpus.id)) + thread.start() flash('Corpus deleted!') return redirect(url_for('main.dashboard')) @@ -132,11 +120,10 @@ def add_corpus_file(corpus_id): title=add_corpus_file_form.title.data) db.session.add(corpus_file) db.session.commit() - background_thread = threading.Thread( - args=(current_app._get_current_object(), corpus_file.id), - target=background_prepare_corpus_file - ) - background_thread.start() + thread = threading.Thread(target=edit_corpus_file_, + args=(current_app._get_current_object(), + corpus_file.id)) + thread.start() flash('Corpus file added!') return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) return render_template('corpora/add_corpus_file.html.j2', @@ -153,8 +140,10 @@ def delete_corpus_file(corpus_id, corpus_file_id): if not (corpus_file.corpus.creator == current_user or current_user.is_administrator()): abort(403) - delete_thread = threading.Thread(corpus_file.delete()) - delete_thread.start() + thread = threading.Thread(target=delete_corpus_file_, + args=(current_app._get_current_object(), + corpus_file.id)) + thread.start() flash('Corpus file deleted!') return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) @@ -188,10 +177,28 @@ def edit_corpus_file(corpus_id, corpus_file_id): corpus_file.author = edit_corpus_file_form.author.data corpus_file.publishing_year = edit_corpus_file_form.publishing_year.data corpus_file.title = edit_corpus_file_form.title.data - corpus_file.insert_metadata() db.session.commit() + thread = threading.Thread(target=edit_corpus_file_, + args=(current_app._get_current_object(), + corpus_file.id)) + thread.start() flash('Corpus file edited!') return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) return render_template('corpora/edit_corpus_file.html.j2', edit_corpus_file_form=edit_corpus_file_form, corpus_file=corpus_file, title='Edit corpus file') + + +@corpora.route('//prepare') +@login_required +def prepare_corpus(corpus_id): + corpus = Corpus.query.get_or_404(corpus_id) + if not (corpus.creator == current_user or current_user.is_administrator()): + abort(403) + if len(corpus.files.all()) > 0: + corpus.status = 'preparable' + db.session.commit() + flash('Corpus marked for preparation!') + else: + flash('Can not prepare corpus, please add corpus file(s).') + return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) diff --git a/app/utils.py b/app/utils.py index 9f74998b..e6676262 100644 --- a/app/utils.py +++ b/app/utils.py @@ -55,18 +55,3 @@ def background_delete_job(app, job_id): job.delete_job() deleted = True logger.warning('Loop has ended.') - - -def background_delete_corpus(app, corpus_id): - logger = logging.getLogger(__name__) - with app.app_context(): - logger.warning('Called by delete_thread.') - logger.warning('Corpus id is: {}.'.format(corpus_id)) - corpus = Corpus.query.filter_by(id=corpus_id).first() - corpus.delete() - - -def background_prepare_corpus_file(app, corpus_file_id): - with app.app_context(): - corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first() - corpus_file.insert_metadata()