Remove job from database if an error occurs. Remove job object from session before thread starts.

This commit is contained in:
Patrick Jentsch 2019-08-08 12:06:01 +02:00
parent 202ade92e9
commit 01c5b3603e

View File

@ -15,7 +15,6 @@ import json
def ocr():
new_ocr_job_form = NewOCRJobForm()
if new_ocr_job_form.validate_on_submit():
app = current_app._get_current_object()
ocr_job = Job(creator=current_user._get_current_object(),
description=new_ocr_job_form.description.data,
service="ocr",
@ -27,10 +26,11 @@ def ocr():
"version": new_ocr_job_form.version.data}),
status="pending",
title=new_ocr_job_form.title.data)
db.session.add(ocr_job)
db.session.commit()
dir = os.path.join(app.config['OPAQUE_STORAGE'],
dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
str(ocr_job.user_id),
'jobs',
str(ocr_job.id))
@ -39,6 +39,8 @@ def ocr():
os.makedirs(dir)
except OSError:
flash('OSError!')
db.session.remove(ocr_job)
db.session.commit()
else:
for file in new_ocr_job_form.files.data:
file.save(os.path.join(dir, file.filename))
@ -48,6 +50,7 @@ def ocr():
' NOTE: Using self created threads is just for testing purpose as
' there is no scheduler available.
'''
db.session.expunge(ocr_job)
thread = Thread(target=swarm.run, args=(ocr_job,))
thread.start()
flash('Job created!')
@ -65,7 +68,6 @@ def ocr():
def nlp():
new_nlp_job_form = NewNLPJobForm()
if new_nlp_job_form.validate_on_submit():
app = current_app._get_current_object()
nlp_job = Job(creator=current_user._get_current_object(),
description=new_nlp_job_form.description.data,
service="nlp",
@ -76,10 +78,11 @@ def nlp():
"version": new_nlp_job_form.version.data}),
status="pending",
title=new_nlp_job_form.title.data)
db.session.add(nlp_job)
db.session.commit()
dir = os.path.join(app.config['OPAQUE_STORAGE'],
dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
str(nlp_job.user_id),
'jobs',
str(nlp_job.id))
@ -88,6 +91,8 @@ def nlp():
os.makedirs(dir)
except OSError:
flash('OSError!')
db.session.remove(nlp_job)
db.session.commit()
else:
for file in new_nlp_job_form.files.data:
file.save(os.path.join(dir, file.filename))
@ -97,6 +102,7 @@ def nlp():
' NOTE: Using self created threads is just for testing purpose as
' there is no scheduler available.
'''
db.session.expunge(nlp_job)
thread = Thread(target=swarm.run, args=(nlp_job,))
thread.start()
flash('Job created!')