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