nopaque/app/utils.py

73 lines
2.6 KiB
Python
Raw Normal View History

from . import db
2019-11-05 09:32:42 +00:00
from .models import Job, User, Corpus, CorpusFile
import logging
'''
' A list of background process functions. Functions should be called using the
Thread class from the module threading.
'''
def background_delete_user(app, current_user_id):
with app.app_context():
logger = logging.getLogger(__name__)
logger.warning('Called by delete_thread.')
logger.warning('User id is: {}.'.format(current_user_id))
jobs = Job.query.filter_by(user_id=current_user_id).all()
corpora = Corpus.query.filter_by(user_id=current_user_id).all()
logger.warning('Jobs to delete are: {}'.format(jobs))
user = User.query.get_or_404(current_user_id)
for job in jobs:
job.flag_for_stop()
logger.warning('Job status: {}'.format(job.status))
deleted = False
while deleted is False:
logger.warning('Refreshing')
db.session.refresh(job)
logger.warning('Refreshed')
if job.status == 'deleted':
logger.warning('Job status is deleted.')
job.delete_job()
deleted = True
logger.warning('Job deletion loop has ended.')
for corpus in corpora:
corpus.delete_corpus()
logger.warning('Corpus deletion loop has ended.')
user.delete_user()
def background_delete_job(app, job_id):
logger = logging.getLogger(__name__)
with app.app_context():
logger.warning('Called by delete_thread.')
logger.warning('Job id is: {}.'.format(job_id))
job = Job.query.filter_by(id=job_id).first()
logger.warning('Job object is: {}'.format(job))
logger.warning('Job status: {}'.format(job.status))
job.flag_for_stop()
logger.warning('Job status: {}'.format(job.status))
deleted = False
while deleted is False:
db.session.refresh(job)
if job.status == 'deleted':
logger.warning('Job status is deleted.')
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()
2019-10-30 07:28:52 +00:00
corpus.delete()
2019-11-04 14:06:54 +00:00
def background_prepare_corpus_file(app, corpus_file_id):
with app.app_context():
corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first()
2019-11-05 09:32:42 +00:00
corpus_file.insert_metadata()