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
|
|
|
|
from .. import db, mail
|
|
|
|
from ..email import create_message
|
2021-02-01 11:51:07 +00:00
|
|
|
from ..models import Job, JobResult
|
2020-11-09 15:14:19 +00:00
|
|
|
import docker
|
|
|
|
import json
|
2021-08-04 15:07:59 +00:00
|
|
|
import logging
|
2020-11-09 15:14:19 +00:00
|
|
|
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()
|
2021-03-26 12:10:42 +00:00
|
|
|
canceling_jobs = list(filter(lambda job: job.status == 'canceling', jobs)) # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
queued_jobs = list(filter(lambda job: job.status == 'queued', jobs))
|
|
|
|
running_jobs = list(filter(lambda job: job.status == 'running', jobs))
|
2021-03-26 12:10:42 +00:00
|
|
|
submitted_jobs = list(filter(lambda job: job.status == 'submitted', jobs)) # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
for job in submitted_jobs:
|
|
|
|
self.create_job_service(job)
|
|
|
|
for job in queued_jobs + running_jobs:
|
|
|
|
self.checkout_job_service(job)
|
|
|
|
for job in canceling_jobs:
|
|
|
|
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':
|
2021-08-04 10:26:49 +00:00
|
|
|
mem_mb = 2048
|
|
|
|
n_cores = 2
|
|
|
|
executable = 'file-setup'
|
2021-08-04 15:07:59 +00:00
|
|
|
image = (current_app.config['DOCKER_IMAGE_PREFIX']
|
|
|
|
+ 'file-setup:' + job.service_version)
|
2021-08-04 10:26:49 +00:00
|
|
|
elif job.service == 'ocr':
|
|
|
|
mem_mb = 4096
|
|
|
|
n_cores = 4
|
|
|
|
executable = 'ocr'
|
2021-08-04 15:07:59 +00:00
|
|
|
image = (current_app.config['DOCKER_IMAGE_PREFIX']
|
|
|
|
+ 'ocr:' + job.service_version)
|
2021-08-04 10:26:49 +00:00
|
|
|
elif job.service == 'nlp':
|
|
|
|
mem_mb = 2048
|
|
|
|
n_cores = 2
|
|
|
|
executable = 'nlp'
|
2021-08-04 15:07:59 +00:00
|
|
|
image = (current_app.config['DOCKER_IMAGE_PREFIX']
|
|
|
|
+ 'nlp:' + job.service_version)
|
|
|
|
''' ## Command ## '''
|
2021-08-04 10:26:49 +00:00
|
|
|
command = '{} -i /input -o /output'.format(executable)
|
|
|
|
command += ' --log-dir /input'
|
|
|
|
command += ' --mem-mb {}'.format(mem_mb)
|
|
|
|
command += ' --n-cores {}'.format(n_cores)
|
|
|
|
command += ' --zip [' + job.service + ']_' + secure_filename(job.title)
|
|
|
|
command += ' ' + ' '.join(json.loads(job.service_args))
|
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 ## '''
|
|
|
|
''' ### Input mount ### '''
|
2021-08-04 10:26:49 +00:00
|
|
|
input_mount_source = job.path
|
|
|
|
input_mount_target = '/input'
|
|
|
|
if job.service == 'file-setup':
|
|
|
|
input_mount_target += '/' + secure_filename(job.title)
|
|
|
|
input_mount = input_mount_source + ':' + input_mount_target + ':rw'
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ### Output mount ### '''
|
2021-08-04 10:26:49 +00:00
|
|
|
output_mount_source = os.path.join(job.path, 'output')
|
|
|
|
output_mount_target = '/output'
|
|
|
|
output_mount = 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)
|
|
|
|
mounts = [input_mount, output_mount]
|
2021-08-04 15:07:59 +00:00
|
|
|
''' ## Name ## '''
|
2021-08-04 10:26:49 +00:00
|
|
|
name = 'job_{}'.format(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:
|
|
|
|
logging.error(
|
2021-08-04 15:07:59 +00:00
|
|
|
'Create "{}" service raised '.format(name)
|
2021-02-01 11:51:07 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2020-11-09 15:14:19 +00:00
|
|
|
return
|
2021-02-01 11:51:07 +00:00
|
|
|
else:
|
|
|
|
job.status = 'queued'
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'replace',
|
|
|
|
'path': '/jobs/{}/status'.format(job.id),
|
|
|
|
'value': job.status
|
|
|
|
}
|
2021-02-01 11:51:07 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation)
|
|
|
|
finally:
|
|
|
|
self.send_job_notification(job)
|
2020-11-09 15:14:19 +00:00
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def checkout_job_service(self, job):
|
|
|
|
service_name = 'job_{}'.format(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:
|
2021-08-04 15:07:59 +00:00
|
|
|
logging.error(
|
|
|
|
'Get "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.NotFound" The service does not exist. '
|
|
|
|
+ '(job.status: {} -> failed)'.format(job.status)
|
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
job.status = 'failed'
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'replace',
|
|
|
|
'path': '/jobs/{}/status'.format(job.id),
|
|
|
|
'value': job.status
|
|
|
|
}
|
2021-02-01 11:51:07 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation)
|
2020-11-13 12:33:32 +00:00
|
|
|
except docker.errors.APIError as e:
|
2020-11-19 11:31:29 +00:00
|
|
|
logging.error(
|
2021-02-01 11:51:07 +00:00
|
|
|
'Get "{}" service raised '.format(service_name)
|
2020-11-19 11:31:29 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2020-11-13 12:33:32 +00:00
|
|
|
return
|
2021-02-01 11:51:07 +00:00
|
|
|
except docker.errors.InvalidVersion:
|
|
|
|
logging.error(
|
|
|
|
'Get "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.InvalidVersion" One of the arguments is '
|
|
|
|
+ 'not supported with the current API version.'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
service_tasks = service.tasks()
|
|
|
|
if not service_tasks:
|
|
|
|
return
|
|
|
|
task_state = service_tasks[0].get('Status').get('State')
|
|
|
|
if job.status == 'queued' and task_state != 'pending':
|
|
|
|
job.status = 'running'
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'replace',
|
|
|
|
'path': '/jobs/{}/status'.format(job.id),
|
|
|
|
'value': job.status
|
|
|
|
}
|
2021-02-01 11:51:07 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation)
|
2021-08-04 15:07:59 +00:00
|
|
|
elif job.status == 'running' and task_state in ['complete', 'failed']: # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
try:
|
|
|
|
service.remove()
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Remove "{}" service raised '.format(service_name)
|
2021-03-26 11:15:33 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. ' # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if task_state == 'complete':
|
|
|
|
results_dir = os.path.join(job.path, 'output')
|
|
|
|
result_files = filter(lambda x: x.endswith('.zip'),
|
|
|
|
os.listdir(results_dir))
|
|
|
|
for result_file in result_files:
|
2021-03-26 11:15:33 +00:00
|
|
|
job_result = JobResult(filename=result_file, job=job) # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
db.session.add(job_result)
|
|
|
|
db.session.flush()
|
|
|
|
db.session.refresh(job_result)
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'add',
|
|
|
|
'path': '/jobs/{}/results/{}'.format(job.id, job_result.id), # noqa
|
|
|
|
'value': job_result.to_dict()
|
|
|
|
}
|
2021-03-26 11:15:33 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation) # noqa
|
2021-02-01 11:51:07 +00:00
|
|
|
job.end_date = datetime.utcnow()
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'replace',
|
|
|
|
'path': '/jobs/{}/end_date'.format(job.id),
|
|
|
|
'value': job.end_date.timestamp()
|
|
|
|
}
|
2021-02-01 11:51:07 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation)
|
|
|
|
job.status = task_state
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'replace',
|
|
|
|
'path': '/jobs/{}/status'.format(job.id),
|
|
|
|
'value': job.status
|
|
|
|
}
|
2021-02-01 11:51:07 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation)
|
|
|
|
finally:
|
|
|
|
self.send_job_notification(job)
|
|
|
|
|
|
|
|
def remove_job_service(self, job):
|
|
|
|
service_name = 'job_{}'.format(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:
|
|
|
|
job.status = 'canceled'
|
2021-08-04 15:07:59 +00:00
|
|
|
patch_operation = {
|
|
|
|
'op': 'replace',
|
|
|
|
'path': '/jobs/{}/status'.format(job.id),
|
|
|
|
'value': job.status
|
|
|
|
}
|
2021-02-01 11:51:07 +00:00
|
|
|
self.buffer_user_patch_operation(job, patch_operation)
|
2020-11-13 12:33:32 +00:00
|
|
|
except docker.errors.APIError as e:
|
2020-11-19 11:31:29 +00:00
|
|
|
logging.error(
|
2021-02-01 11:51:07 +00:00
|
|
|
'Get "{}" service raised '.format(service_name)
|
2020-11-19 11:31:29 +00:00
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
return
|
|
|
|
except docker.errors.InvalidVersion:
|
|
|
|
logging.error(
|
|
|
|
'Get "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.InvalidVersion" One of the arguments is '
|
|
|
|
+ 'not supported with the current API version.'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
service.update(mounts=None)
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Update "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
service.remove()
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
logging.error(
|
|
|
|
'Remove "{}" service raised '.format(service_name)
|
|
|
|
+ '"docker.errors.APIError" The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e)
|
|
|
|
)
|
2020-11-19 08:41:22 +00:00
|
|
|
|
2021-02-01 11:51:07 +00:00
|
|
|
def send_job_notification(self, job):
|
|
|
|
if job.creator.setting_job_status_mail_notifications == 'none':
|
|
|
|
return
|
|
|
|
if (job.creator.setting_job_status_mail_notifications == 'end'
|
|
|
|
and job.status not in ['complete', 'failed']):
|
|
|
|
return
|
2021-08-04 15:07:59 +00:00
|
|
|
msg = create_message(
|
|
|
|
job.creator.email,
|
|
|
|
'Status update for your Job "{}"'.format(job.title), # noqa
|
|
|
|
'tasks/email/notification',
|
|
|
|
job=job
|
|
|
|
)
|
2021-02-01 11:51:07 +00:00
|
|
|
mail.send(msg)
|