2022-02-08 11:26:20 +00:00
|
|
|
from app import db
|
|
|
|
from app.models import Job, JobResult, JobStatus, TesseractOCRModel
|
2020-11-09 15:14:19 +00:00
|
|
|
from datetime import datetime
|
2021-08-04 15:07:59 +00:00
|
|
|
from flask import current_app
|
2020-11-13 12:33:32 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
2020-11-09 15:14:19 +00:00
|
|
|
import docker
|
|
|
|
import json
|
|
|
|
import os
|
2021-08-04 15:07:59 +00:00
|
|
|
import shutil
|
2021-03-26 12:10:42 +00:00
|
|
|
|
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
class CheckJobsMixin:
|
|
|
|
def check_jobs(self):
|
|
|
|
jobs = Job.query.all()
|
2022-02-08 11:26:20 +00:00
|
|
|
for job in (x for x in jobs if x.status == JobStatus.SUBMITTED):
|
2021-02-01 11:51:07 +00:00
|
|
|
self.create_job_service(job)
|
2022-02-08 11:26:20 +00:00
|
|
|
for job in (x for x in jobs if x.status in [JobStatus.QUEUED, JobStatus.RUNNING]): # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
self.checkout_job_service(job)
|
2022-02-08 11:26:20 +00:00
|
|
|
for job in (x for x in jobs if x.status == JobStatus.CANCELING):
|
2021-02-01 11:51:07 +00:00
|
|
|
self.remove_job_service(job)
|
2020-11-09 15:14:19 +00:00
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def create_job_service(self, job):
|
2021-08-04 15:07:59 +00:00
|
|
|
''' # Docker service settings # '''
|
|
|
|
''' ## Service specific settings ## '''
|
2021-04-14 10:00:09 +00:00
|
|
|
if job.service == 'file-setup':
|
2022-02-03 11:39:16 +00:00
|
|
|
mem_mb = 512
|
2021-08-04 10:26:49 +00:00
|
|
|
n_cores = 2
|
|
|
|
executable = 'file-setup'
|
2022-02-03 11:39:16 +00:00
|
|
|
image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}file-setup:v{job.service_version}' # noqa
|
|
|
|
elif job.service == 'tesseract-ocr':
|
|
|
|
mem_mb = 2048
|
2021-08-04 10:26:49 +00:00
|
|
|
n_cores = 4
|
|
|
|
executable = 'ocr'
|
2022-02-03 11:39:16 +00:00
|
|
|
image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}ocr:v{job.service_version}' # noqa
|
|
|
|
elif job.service == 'spacy-nlp':
|
|
|
|
mem_mb = 1024
|
|
|
|
n_cores = 1
|
2021-08-04 10:26:49 +00:00
|
|
|
executable = 'nlp'
|
2022-02-03 11:39:16 +00:00
|
|
|
image = f'{current_app.config["NOPAQUE_DOCKER_IMAGE_PREFIX"]}nlp:v{job.service_version}' # noqa
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Command ## '''
|
2021-11-16 14:23:57 +00:00
|
|
|
command = f'{executable} -i /input -o /output'
|
2022-02-03 11:39:16 +00:00
|
|
|
command += ' --log-dir /logs'
|
2021-11-16 14:23:57 +00:00
|
|
|
command += f' --mem-mb {mem_mb}'
|
|
|
|
command += f' --n-cores {n_cores}'
|
2022-02-03 11:39:16 +00:00
|
|
|
service_args = json.loads(job.service_args)
|
|
|
|
if job.service == 'spacy-nlp':
|
|
|
|
command += f' -m {service_args["model"]}'
|
|
|
|
if 'encoding_detection' in service_args and service_args['encoding_detection']: # noqa
|
|
|
|
command += ' --check-encoding'
|
|
|
|
elif job.service == 'tesseract-ocr':
|
|
|
|
command += f' -m {service_args["model"]}'
|
|
|
|
if 'binarization' in service_args and service_args['binarization']:
|
|
|
|
command += ' --binarize'
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Constraints ## '''
|
2021-08-04 10:26:49 +00:00
|
|
|
constraints = ['node.role==worker']
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Labels ## '''
|
|
|
|
labels = {
|
|
|
|
'origin': current_app.config['SERVER_NAME'],
|
|
|
|
'type': 'job',
|
|
|
|
'job_id': str(job.id)
|
|
|
|
}
|
|
|
|
''' ## Mounts ## '''
|
2022-02-03 11:39:16 +00:00
|
|
|
mounts = []
|
|
|
|
''' ### Input mount(s) ### '''
|
|
|
|
input_mount_target_base = '/input'
|
2021-08-04 10:26:49 +00:00
|
|
|
if job.service == 'file-setup':
|
2022-02-03 11:39:16 +00:00
|
|
|
input_mount_target_base += f'/{secure_filename(job.title)}'
|
|
|
|
for job_input in job.inputs:
|
|
|
|
input_mount_source = job_input.path
|
|
|
|
input_mount_target = f'/{input_mount_target_base}/{job_input.filename}' # noqa
|
|
|
|
input_mount = f'{input_mount_source}:{input_mount_target}:ro'
|
|
|
|
mounts.append(input_mount)
|
|
|
|
if job.service == 'tesseract-ocr':
|
|
|
|
service_args = json.loads(job.service_args)
|
|
|
|
model = TesseractOCRModel.query.get(service_args['model'])
|
|
|
|
if model is None:
|
2022-02-08 11:26:20 +00:00
|
|
|
job.status = JobStatus.FAILED
|
2022-02-03 11:39:16 +00:00
|
|
|
return
|
|
|
|
models_mount_source = model.path
|
|
|
|
models_mount_target = f'/usr/local/share/tessdata/{model.filename}'
|
|
|
|
models_mount = f'{models_mount_source}:{models_mount_target}:ro'
|
|
|
|
mounts.append(models_mount)
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ### Output mount ### '''
|
2022-02-03 11:39:16 +00:00
|
|
|
output_mount_source = os.path.join(job.path, 'results')
|
2021-08-04 10:26:49 +00:00
|
|
|
output_mount_target = '/output'
|
2021-11-16 14:23:57 +00:00
|
|
|
output_mount = f'{output_mount_source}:{output_mount_target}:rw'
|
2021-08-04 15:07:59 +00:00
|
|
|
# Make sure that their is no data in the output directory
|
|
|
|
shutil.rmtree(output_mount_source, ignore_errors=True)
|
2021-08-04 10:26:49 +00:00
|
|
|
os.makedirs(output_mount_source)
|
2022-02-03 11:39:16 +00:00
|
|
|
mounts.append(output_mount)
|
|
|
|
''' ### Pipeline data mount ### '''
|
|
|
|
pyflow_data_mount_source = os.path.join(job.path, 'pipeline_data')
|
|
|
|
pyflow_data_mount_target = '/logs/pyflow.data'
|
|
|
|
pyflow_data_mount = f'{pyflow_data_mount_source}:{pyflow_data_mount_target}:rw' # noqa
|
|
|
|
# Make sure that their is no data in the output directory
|
|
|
|
shutil.rmtree(pyflow_data_mount_source, ignore_errors=True)
|
|
|
|
os.makedirs(pyflow_data_mount_source)
|
|
|
|
mounts.append(pyflow_data_mount)
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Name ## '''
|
2021-11-16 14:23:57 +00:00
|
|
|
name = f'job_{job.id}'
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Resources ## '''
|
2021-08-04 10:26:49 +00:00
|
|
|
resources = docker.types.Resources(
|
|
|
|
cpu_reservation=n_cores * (10 ** 9),
|
|
|
|
mem_reservation=mem_mb * (10 ** 6)
|
|
|
|
)
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Restart policy ## '''
|
2021-08-04 10:26:49 +00:00
|
|
|
restart_policy = docker.types.RestartPolicy()
|
2021-02-01 11:51:07 +00:00
|
|
|
try:
|
2021-08-04 10:26:49 +00:00
|
|
|
self.docker.services.create(
|
|
|
|
image,
|
|
|
|
command=command,
|
|
|
|
constraints=constraints,
|
|
|
|
labels=labels,
|
|
|
|
mounts=mounts,
|
|
|
|
name=name,
|
|
|
|
resources=resources,
|
|
|
|
restart_policy=restart_policy
|
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
except docker.errors.APIError as e:
|
2021-09-16 09:15:31 +00:00
|
|
|
current_app.logger.error(
|
2021-11-16 14:23:57 +00:00
|
|
|
f'Create service "{name}" failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.APIError": {e}'
|
2021-02-01 11:51:07 +00:00
|
|
|
)
|
2020-11-09 15:14:19 +00:00
|
|
|
return
|
2022-02-08 11:26:20 +00:00
|
|
|
job.status = JobStatus.QUEUED
|
2020-11-09 15:14:19 +00:00
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def checkout_job_service(self, job):
|
2021-11-16 14:23:57 +00:00
|
|
|
service_name = f'job_{job.id}'
|
2020-11-13 12:33:32 +00:00
|
|
|
try:
|
2021-02-01 11:51:07 +00:00
|
|
|
service = self.docker.services.get(service_name)
|
2021-11-16 14:23:57 +00:00
|
|
|
except docker.errors.NotFound as e:
|
2021-09-16 09:15:31 +00:00
|
|
|
current_app.logger.error(
|
2021-11-16 14:23:57 +00:00
|
|
|
f'Get service "{service_name}" failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.NotFound": {e}'
|
2021-08-04 15:07:59 +00:00
|
|
|
)
|
2022-02-08 11:26:20 +00:00
|
|
|
job.status = JobStatus.FAILED
|
2021-11-16 14:23:57 +00:00
|
|
|
return
|
2020-11-13 12:33:32 +00:00
|
|
|
except docker.errors.APIError as e:
|
2021-09-16 09:15:31 +00:00
|
|
|
current_app.logger.error(
|
2021-11-16 14:23:57 +00:00
|
|
|
f'Get service "{service_name}" failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.APIError": {e}'
|
2020-11-19 11:31:29 +00:00
|
|
|
)
|
2020-11-13 12:33:32 +00:00
|
|
|
return
|
2021-11-16 14:23:57 +00:00
|
|
|
service_tasks = service.tasks()
|
|
|
|
if not service_tasks:
|
2021-02-01 11:51:07 +00:00
|
|
|
return
|
2021-11-16 14:23:57 +00:00
|
|
|
task_state = service_tasks[0].get('Status').get('State')
|
2022-02-08 11:26:20 +00:00
|
|
|
if job.status == JobStatus.QUEUED and task_state != 'pending':
|
|
|
|
job.status = JobStatus.RUNNING
|
2021-11-16 14:23:57 +00:00
|
|
|
return
|
2022-02-08 11:26:20 +00:00
|
|
|
elif job.status == JobStatus.RUNNING and task_state == 'complete': # noqa
|
|
|
|
job.status = JobStatus.COMPLETED
|
2022-02-03 11:39:16 +00:00
|
|
|
results_dir = os.path.join(job.path, 'results')
|
|
|
|
with open(os.path.join(results_dir, 'outputs.json')) as f:
|
|
|
|
outputs = json.load(f)
|
|
|
|
for output in outputs:
|
|
|
|
filename = os.path.basename(output['file'])
|
|
|
|
job_result = JobResult(
|
|
|
|
filename=filename,
|
|
|
|
job=job,
|
|
|
|
mimetype=output['mimetype']
|
|
|
|
)
|
|
|
|
if 'description' in output:
|
|
|
|
job_result.description = output['description']
|
2021-11-16 14:23:57 +00:00
|
|
|
db.session.add(job_result)
|
2022-02-03 11:39:16 +00:00
|
|
|
db.session.flush(objects=[job_result])
|
2021-11-16 14:23:57 +00:00
|
|
|
db.session.refresh(job_result)
|
2022-02-03 11:39:16 +00:00
|
|
|
os.rename(
|
|
|
|
os.path.join(results_dir, output['file']),
|
|
|
|
job_result.path
|
|
|
|
)
|
2022-02-08 11:26:20 +00:00
|
|
|
elif job.status == JobStatus.RUNNING and task_state == 'failed':
|
|
|
|
job.status = JobStatus.FAILED
|
2021-02-01 11:51:07 +00:00
|
|
|
else:
|
2021-11-16 14:23:57 +00:00
|
|
|
return
|
|
|
|
job.end_date = datetime.utcnow()
|
|
|
|
try:
|
|
|
|
service.remove()
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
f'Remove service "{service_name}" failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.APIError": {e}'
|
2021-11-16 14:23:57 +00:00
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
|
|
|
|
def remove_job_service(self, job):
|
2021-11-16 14:23:57 +00:00
|
|
|
service_name = f'job_{job.id}'
|
2020-11-13 12:33:32 +00:00
|
|
|
try:
|
2021-02-01 11:51:07 +00:00
|
|
|
service = self.docker.services.get(service_name)
|
|
|
|
except docker.errors.NotFound:
|
2022-02-08 11:26:20 +00:00
|
|
|
job.status = JobStatus.CANCELED
|
2021-11-16 14:23:57 +00:00
|
|
|
return
|
2020-11-13 12:33:32 +00:00
|
|
|
except docker.errors.APIError as e:
|
2021-09-16 09:15:31 +00:00
|
|
|
current_app.logger.error(
|
2021-11-16 14:23:57 +00:00
|
|
|
f'Get service "{service_name}" failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.APIError": {e}'
|
2020-11-19 11:31:29 +00:00
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
return
|
2021-11-16 14:23:57 +00:00
|
|
|
try:
|
|
|
|
service.update(mounts=None)
|
|
|
|
except docker.errors.APIError as e:
|
2021-09-16 09:15:31 +00:00
|
|
|
current_app.logger.error(
|
2021-11-16 14:23:57 +00:00
|
|
|
f'Update service "{service_name}" failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.APIError": {e}'
|
2021-02-01 11:51:07 +00:00
|
|
|
)
|
|
|
|
return
|
2021-11-16 14:23:57 +00:00
|
|
|
try:
|
|
|
|
service.remove()
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
f'Remove "{service_name}" service failed '
|
2022-02-03 11:39:16 +00:00
|
|
|
f'due to "docker.errors.APIError": {e}'
|
2021-11-16 14:23:57 +00:00
|
|
|
)
|