nopaque/web/app/tasks/job_utils.py

137 lines
5.7 KiB
Python
Raw Normal View History

2020-11-09 15:14:19 +00:00
from datetime import datetime
2020-11-13 12:33:32 +00:00
from werkzeug.utils import secure_filename
2020-11-09 15:14:19 +00:00
from . import docker_client
2020-11-13 12:33:32 +00:00
from .. import db, mail
from ..email import create_message
2020-11-09 15:14:19 +00:00
from ..models import JobResult
import docker
import logging
import json
import os
def create_job_service(job):
cmd = '{} -i /files -o /files/output'.format(job.service)
if job.service == 'file-setup':
2020-11-13 12:33:32 +00:00
cmd += ' -f {}'.format(secure_filename(job.title))
2020-11-09 15:14:19 +00:00
cmd += ' --log-dir /files'
2020-11-13 12:33:32 +00:00
cmd += ' --zip [{}]_{}'.format(job.service, secure_filename(job.title))
2020-11-09 15:14:19 +00:00
cmd += ' ' + ' '.join(json.loads(job.service_args))
service_kwargs = {'command': cmd,
'constraints': ['node.role==worker'],
'labels': {'origin': 'nopaque',
'type': 'service.{}'.format(job.service),
'job_id': str(job.id)},
'mounts': [job.path + ':/files:rw'],
'name': 'job_{}'.format(job.id),
'resources': docker.types.Resources(
cpu_reservation=job.n_cores * (10 ** 9),
mem_reservation=job.mem_mb * (10 ** 6)
),
'restart_policy': docker.types.RestartPolicy()}
2020-11-09 15:14:19 +00:00
service_image = ('gitlab.ub.uni-bielefeld.de:4567/sfb1288inf/'
+ job.service + ':' + job.service_version)
try:
docker_client.services.create(service_image, **service_kwargs)
2020-11-09 15:14:19 +00:00
except docker.errors.APIError as e:
logging.error('Create "{}" service raised '.format(service_kwargs['name']) # noqa
+ '[docker-APIError] The server returned an error. '
+ 'Details: {}'.format(e))
2020-11-13 12:33:32 +00:00
return
2020-11-09 15:14:19 +00:00
else:
job.status = 'queued'
2020-11-13 12:33:32 +00:00
finally:
msg = create_message(
job.creator.email,
'Status update for your Job "{}"'.format(job.title),
'tasks/email/notification',
job=job
)
2020-11-13 12:33:32 +00:00
mail.send(msg)
2020-11-09 15:14:19 +00:00
def checkout_job_service(job):
service_name = 'job_{}'.format(job.id)
try:
service = docker_client.services.get(service_name)
except docker.errors.NotFound:
logging.error('Get "{}" service raised '.format(service_name)
+ '[docker-NotFound] The service does not exist. '
+ '(job.status: {} -> failed)'.format(job.status))
job.status = 'failed'
except docker.errors.APIError as e:
logging.error('Get "{}" service raised '.format(service_name)
+ '[docker-APIError] The server returned an error. '
+ 'Details: {}'.format(e))
2020-11-13 12:33:32 +00:00
return
except docker.errors.InvalidVersion:
logging.error('Get "{}" service raised '.format(service_name)
+ '[docker-InvalidVersion] One of the arguments is '
+ 'not supported with the current API version.')
2020-11-13 12:33:32 +00:00
return
2020-11-09 15:14:19 +00:00
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'
2020-11-13 12:33:32 +00:00
elif job.status == 'running' and task_state in ['complete', 'failed']: # noqa
try:
service.remove()
except docker.errors.APIError as e:
logging.error('Remove "{}" service raised '.format(service_name) # noqa
+ '[docker-APIError] The server returned an error. ' # noqa
+ 'Details: {}'.format(e))
return
else:
job.end_date = datetime.utcnow()
job.status = task_state
if task_state == 'complete':
job_results_dir = os.path.join(job.path, 'output')
job_results = filter(lambda x: x.endswith('.zip'),
os.listdir(job_results_dir))
for job_result in job_results:
job_result = JobResult(filename=job_result, job=job)
db.session.add(job_result)
2020-11-09 15:14:19 +00:00
finally:
msg = create_message(
job.creator.email,
2020-11-13 12:33:32 +00:00
'Status update for your Job "{}"'.format(job.title),
'tasks/email/notification',
job=job
)
2020-11-13 12:33:32 +00:00
mail.send(msg)
2020-11-09 15:14:19 +00:00
def remove_job_service(job):
service_name = 'job_{}'.format(job.id)
try:
service = docker_client.services.get(service_name)
except docker.errors.NotFound:
job.status = 'canceled'
2020-11-13 12:33:32 +00:00
except docker.errors.APIError as e:
logging.error('Get "{}" service raised '.format(service_name)
+ '[docker-APIError] The server returned an error. '
+ 'Details: {}'.format(e))
return
except docker.errors.InvalidVersion:
logging.error('Get "{}" service raised '.format(service_name)
+ '[docker-InvalidVersion] One of the arguments is '
+ 'not supported with the current API version.')
return
2020-11-09 15:14:19 +00:00
else:
2020-11-13 12:33:32 +00:00
try:
service.update(mounts=None)
except docker.errors.APIError as e:
logging.error('Update "{}" service raised '.format(service_name) # noqa
+ '[docker-APIError] The server returned an error. ' # noqa
+ 'Details: {}'.format(e))
return
try:
service.remove()
except docker.errors.APIError as e:
logging.error('Remove "{}" service raised '.format(service_name) # noqa
+ '[docker-APIError] The server returned an error. ' # noqa
+ 'Details: {}'.format(e))