from mesh.standard import *
from scheme import *
from platoon.constants import *

TaskStructure = Structure(
    structure={
        'http-request': {
            'url': Text(nonempty=True),
            'method': Text(nonempty=True),
            'mimetype': Text(),
            'data': Text(),
            'headers': Map(Text(nonempty=True)),
            'timeout': Integer(nonnull=True, default=30),
            'injections': Sequence(Text(nonempty=True)),
        },
        'internal': {
            'purpose': Enumeration('purge', nonempty=True),
        },
        'test': {
            'status': Enumeration('complete fail exception', nonempty=True),
            'result': Text(),
        },
        'process': {
            'action': Enumeration(list(PROCESS_TASK_ACTIONS), nonempty=True),
            'process_id': UUID(nonempty=True),
        }
    },
    polymorphic_on=Enumeration('http-request internal test process', name='type', nonempty=True),
    nonnull=True)

class Task(Resource):
    """A queue task."""

    class schema:
        id = UUID(operators='equal')
        tag = Text(nonempty=True, operators='equal')
        description = Text()
        retry_backoff = Float()
        retry_limit = Integer(nonnull=True, default=2)
        retry_timeout = Integer(nonnull=True, default=300)
        task = TaskStructure.clone(required=True)
        completed = TaskStructure.clone()
        failed = TaskStructure.clone()
        created = DateTime(utc=True, readonly=True)
