Ejemplo n.º 1
0
def business_create(task: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]:
    """Creates new Task db record.
    Fields which require to be of datetime type are explicitly converted here.
    """
    try:
        new_task = Task(
            user_id=task['userId'],
            host=task['hostname'],
            command=task['command'],
            # TODO Adjust API spec, optional fields
            spawn_at=try_parse_input_datetime(task.get('spawnAt')),
            terminate_at=try_parse_input_datetime(task.get('terminateAt')))
        # assert all(task.values()), 'fields cannot be blank or null'
        new_task.save()
    except ValueError:
        # Invalid string format for datetime
        content, status = {'msg': G['bad_request']}, 422
    except KeyError:
        # At least one of required fields was not present
        content, status = {'msg': G['bad_request']}, 422
    except AssertionError as e:
        content, status = {
            'msg': T['create']['failure']['invalid'].format(reason=e)
        }, 422
    except Exception as e:
        log.critical(e)
        content, status = {'msg': G['internal_error']}, 500
    else:
        content, status = {
            'msg': T['create']['success'],
            'task': new_task.as_dict
        }, 201
    finally:
        return content, status
Ejemplo n.º 2
0
def new_task():
    task = Task(command='python command.py',
                hostname='localhost',
                _status=TaskStatus.not_running)
    cmd_segment = CommandSegment(name='--batch_size',
                                 _segment_type=SegmentType.parameter)
    task.add_cmd_segment(cmd_segment, '32')
    task.save()
    return task
Ejemplo n.º 3
0
def new_task_2():
    task = Task(command='python command2.py',
                hostname='remotehost',
                _status=TaskStatus.not_running)
    cmd_segment = CommandSegment(name='--rank',
                                 _segment_type=SegmentType.parameter)
    task.add_cmd_segment(cmd_segment, '1')
    task.save()
    return task
Ejemplo n.º 4
0
def business_create(task: Dict[str, Any], job_id: JobId) -> Tuple[Content, HttpStatusCode]:
    """ Creates new Task db record under the given parent job
    and new db records for given command segments of that task.
    """
    try:
        new_task = Task(
            hostname=task['hostname'],
            command=task['command'])
        new_task.gpu_id = parse_gpu_id_from_command(task['command'])
        parent_job = Job.query.filter(Job.id == job_id).one()
        for segment in task['cmdsegments']['params']:
            new_segment = CommandSegment.query.filter(CommandSegment.segment_type == SegmentType.parameter,
                                                      CommandSegment.name == segment['name']).first()
            if (new_segment is None):
                new_segment = CommandSegment(
                    name=segment['name'],
                    _segment_type=SegmentType.parameter)
            new_task.add_cmd_segment(new_segment, segment['value'])
        for segment in task['cmdsegments']['envs']:
            new_segment = CommandSegment.query.filter(CommandSegment.segment_type == SegmentType.env_variable,
                                                      CommandSegment.name == segment['name']).first()
            if (new_segment is None):
                new_segment = CommandSegment(
                    name=segment['name'],
                    _segment_type=SegmentType.env_variable)
            new_task.add_cmd_segment(new_segment, segment['value'])

        new_task.save()
        parent_job.add_task(new_task)
    except KeyError:
        # At least one of required fields was not present
        content, status = {'msg': GENERAL['bad_request']}, 422
    except AssertionError as e:
        content, status = {'msg': TASK['create']['failure']['invalid'].format(reason=e)}, 422
    except Exception as e:
        log.critical(e)
        content, status = {'msg': GENERAL['internal_error']}, 500
    else:
        content, status = {'msg': TASK['create']['success'], 'task': new_task.as_dict()}, 201
    finally:
        return content, status