nopaque/web/app/tasks/job_utils.py

153 lines
5.8 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-19 11:31:29 +00:00
service_image = 'gitlab.ub.uni-bielefeld.de:4567/sfb1288inf/{}:{}'.format(
job.service, job.service_version)
2020-11-09 15:14:19 +00:00
try:
docker_client.services.create(service_image, **service_kwargs)
2020-11-09 15:14:19 +00:00
except docker.errors.APIError as e:
2020-11-19 11:31:29 +00:00
logging.error(
'Create "{}" service raised '.format(service_kwargs['name'])
+ '"docker.errors.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:
2020-11-19 08:41:22 +00:00
send_notification(job)
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)
2020-11-19 11:31:29 +00:00
+ '"docker.errors.NotFound" The service does not exist. '
+ '(job.status: {} -> failed)'.format(job.status))
job.status = 'failed'
except docker.errors.APIError as e:
2020-11-19 11:31:29 +00:00
logging.error(
'Get "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e)
)
2020-11-13 12:33:32 +00:00
return
except docker.errors.InvalidVersion:
2020-11-19 11:31:29 +00:00
logging.error(
'Get "{}" service raised '.format(service_name)
+ '"docker.errors.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-19 11:31:29 +00:00
elif job.status == 'running' and task_state in ['complete', 'failed']:
2020-11-13 12:33:32 +00:00
try:
service.remove()
except docker.errors.APIError as e:
2020-11-19 11:31:29 +00:00
logging.error(
'Remove "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e)
)
2020-11-13 12:33:32 +00:00
return
else:
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-19 08:41:22 +00:00
job.end_date = datetime.utcnow()
job.status = task_state
2020-11-09 15:14:19 +00:00
finally:
2020-11-19 08:41:22 +00:00
send_notification(job)
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:
2020-11-19 11:31:29 +00:00
logging.error(
'Get "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e)
)
2020-11-13 12:33:32 +00:00
return
except docker.errors.InvalidVersion:
2020-11-19 11:31:29 +00:00
logging.error(
'Get "{}" service raised '.format(service_name)
+ '"docker.errors.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:
2020-11-13 12:33:32 +00:00
try:
service.update(mounts=None)
except docker.errors.APIError as e:
2020-11-19 11:31:29 +00:00
logging.error(
'Update "{}" service raised '.format(service_name)
+ '"docker.errors.APIError" The server returned an error. '
+ 'Details: {}'.format(e)
)
2020-11-13 12:33:32 +00:00
return
try:
service.remove()
except docker.errors.APIError as e:
2020-11-19 11:31:29 +00:00
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
def send_notification(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
msg = create_message(job.creator.email,
'Status update for your Job "{}"'.format(job.title),
'tasks/email/notification', job=job)
mail.send(msg)