2020-11-09 15:14:19 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from . import docker_client
|
|
|
|
from .. import db
|
2020-11-13 09:01:51 +00:00
|
|
|
from ..email import create_message, send
|
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':
|
|
|
|
cmd += ' -f {}'.format(job.secure_filename)
|
|
|
|
cmd += ' --log-dir /files'
|
|
|
|
cmd += ' --zip [{}]_{}'.format(job.service, job.secure_filename)
|
|
|
|
cmd += ' ' + ' '.join(json.loads(job.service_args))
|
2020-11-13 09:01:51 +00:00
|
|
|
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:
|
2020-11-13 09:01:51 +00:00
|
|
|
docker_client.services.create(service_image, **service_kwargs)
|
2020-11-09 15:14:19 +00:00
|
|
|
except docker.errors.APIError as e:
|
2020-11-13 09:01:51 +00:00
|
|
|
logging.error('Create "{}" service raised '.format(service_kwargs['name']) # noqa
|
|
|
|
+ '[docker-APIError] The server returned an error. '
|
|
|
|
+ 'Details: {}'.format(e))
|
2020-11-09 15:14:19 +00:00
|
|
|
else:
|
|
|
|
job.status = 'queued'
|
2020-11-13 09:01:51 +00:00
|
|
|
msg = create_message(
|
|
|
|
job.creator.email,
|
|
|
|
'Status update for your Job "{}"'.format(job.title),
|
|
|
|
'tasks/email/notification',
|
|
|
|
job=job
|
|
|
|
)
|
|
|
|
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)
|
2020-11-13 09:01:51 +00:00
|
|
|
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))
|
|
|
|
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-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 09:01:51 +00:00
|
|
|
elif job.status == 'running' and task_state == 'complete':
|
2020-11-09 15:14:19 +00:00
|
|
|
service.remove()
|
|
|
|
job.end_date = datetime.utcnow()
|
|
|
|
job.status = task_state
|
|
|
|
if task_state == 'complete':
|
2020-11-13 09:01:51 +00:00
|
|
|
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)
|
2020-11-09 15:14:19 +00:00
|
|
|
db.session.add(job_result)
|
|
|
|
elif job.status == 'running' and task_state == 'failed':
|
|
|
|
service.remove()
|
|
|
|
job.end_date = datetime.utcnow()
|
|
|
|
job.status = task_state
|
|
|
|
finally:
|
|
|
|
# TODO: send email
|
2020-11-13 09:01:51 +00:00
|
|
|
msg = create_message(
|
|
|
|
job.creator.email,
|
|
|
|
'[nopaque] Status update for your Job "{}"'.format(job.title),
|
|
|
|
'tasks/email/notification',
|
|
|
|
job=job
|
|
|
|
)
|
|
|
|
send(msg)
|
2020-11-09 15:14:19 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def remove_job_service(job):
|
|
|
|
service_name = 'job_{}'.format(job.id)
|
|
|
|
try:
|
|
|
|
service = docker_client.services.get(service_name)
|
|
|
|
except docker.errors.NotFound:
|
|
|
|
# TODO: send email
|
|
|
|
job.status = 'canceled'
|
|
|
|
# TODO: handle docker.errors.APIError and docker.errors.InvalidVersion
|
|
|
|
else:
|
|
|
|
service.update(mounts=None)
|
|
|
|
service.remove()
|