Source

nx-web / nxweb / apps / daac / views / __init__.py

Full commit
import colander

from pyramid.response import Response

import nxweb.data.dkvs.task as task
import nxweb.data.dkvs.program as program

from nxweb.data.dkvs import compose_key, split_key

from nxweb.utils.ws.response import (ok, error)
from nxweb.utils.ws.decorators import validate
from nxweb.utils.ws.validators import validate_colander_schema
from nxweb.utils.colander_node_validators import uuid4_str_validator

# TODO: temp
from nxweb.apps.cmanws import send_event__task_completed


def program_get(req):
    if req.headers.get('binary') is not None:
        #return program binary
        program_key = req.headers.get('program_key')
        binary = program.get_binary(program_key)
        return Response(body=binary, content_type='application/octet-stream')
    else:
        #return program metadata
        program_key = req.headers.get('program_key')
        return ok(req, data=program.get_metadata(program_key))


def input_get(req):
    task_key = req.headers.get('task_key')
    worker_token = req.headers.get('worker_token')
    input_data_chunk = task.get_input_data(task_key, worker_token)
    return Response(body=input_data_chunk, content_type='application/octet-stream')


def input_post(req):
    """TODO: el input tambien se puede setear al crear la tarea en el client manager,
    por lo que no es urgente definir esta funcion"""
    pass


task_id_schema = colander.SchemaNode(colander.Mapping())
task_id_schema.add(colander.SchemaNode(colander.String(), name='task_id', location='path', validator=uuid4_str_validator))


@validate(validate_colander_schema(task_id_schema))
def output_get(req):
    pkid = req.authenticated_pkid
    task_id = req.matchdict['task_id']

    task_key = compose_key(pkid, task_id)

    output_data_chunk = task.get_output_data(task_key)

    if output_data_chunk is not None:
        return Response(body=output_data_chunk, content_type='application/octet-stream')
    else:
        return error(req, '404 Task "ouput data" not found')


def output_post(req):
    task_key = req.headers.get('task_key')
    worker_token = req.headers.get('worker_token')
    data = req.body
    task.write_output_data(task_key, worker_token, data)
    task.set_error_status(task_key, worker_token)

    # TODO: remover codigo temporal: activar evento de tarea completada
    pkid, task_id = split_key(task_key)
    send_event__task_completed(pkid, task_id)

    return ok(req)