def request_tasks():
    data = request.json

    if data is None:
        current_app.logger.error('No JSON data provided with request.')
        abort(400)

    try:
        jsonschema.validate(data, TASK_REQUEST_SCHEMA)
    except jsonschema.ValidationError:
        current_app.logger.error('Invalid JSON data provided with request.')
        abort(400)

    protocol = int(data['protocol'])
    agent_uuid = str(data['agent_id'])
    agent_name = str(data['agent_name'])
    agent_capabilities = data['agent_capabilities']
    max_tasks = int(data['max_tasks'])
    # agent_time = data['agent_time']
    # agent_location = data['agent_location'] if 'agent_location' in data else None

    # Only protocol 1 supported for now
    if protocol != 1:
        abort(400)

    # Update agent details in DB
    agent = Agent.get_agent(agent_uuid, agent_name)
    agent.update_capabilities(agent_capabilities)
    agent.last_seen = datetime.utcnow()

    # Calculate return time for the agent (next polling time)
    return_time = (
        datetime.now(tz.tzlocal()) +
        timedelta(0, current_app.config.get('AGENT_RETURN_TIME'))).isoformat()

    # Claim tasks for agent
    tasks = [{
        'task_id': task.uuid,
        'task_type': task.type,
        'task_version': task.version,
        'task_data': json.loads(task.data)
    } for task in Task.claim_tasks(agent, max_tasks)]
    if len(tasks) > 0:
        current_app.logger.info("Assigning tasks {} to agent {}, {}".format(
            [task['task_id'] for task in tasks], agent_name, agent_uuid))
        current_app.logger.debug("Task details: {}".format(tasks))

    response = jsonify(tasks=tasks, return_time=return_time)

    # commit only after serializing the response
    db.session.commit()

    return response
def request_tasks():
    data = request.json

    if data is None:
        current_app.logger.error('No JSON data provided with request.')
        abort(400)

    try:
        jsonschema.validate(data, TASK_REQUEST_SCHEMA)
    except jsonschema.ValidationError:
        current_app.logger.error('Invalid JSON data provided with request.')
        abort(400)

    protocol = int(data['protocol'])
    agent_uuid = str(data['agent_id'])
    agent_name = str(data['agent_name'])
    agent_capabilities = data['agent_capabilities']
    max_tasks = int(data['max_tasks'])
    # agent_time = data['agent_time']
    # agent_location = data['agent_location'] if 'agent_location' in data else None

    # Only protocol 1 supported for now
    if protocol != 1:
        abort(400)

    # Update agent details in DB
    agent = Agent.get_agent(agent_uuid, agent_name)
    agent.update_capabilities(agent_capabilities)
    agent.last_seen = datetime.utcnow()

    # Calculate return time for the agent (next polling time)
    return_time = (datetime.now(tz.tzlocal()) + timedelta(0, current_app.config.get('AGENT_RETURN_TIME'))).isoformat()

    # Claim tasks for agent
    tasks = [{'task_id': task.uuid, 'task_type': task.type, 'task_version': task.version,
              'task_data': json.loads(task.data)} for task in Task.claim_tasks(agent, max_tasks)]
    if len(tasks) > 0:
        current_app.logger.info("Assigning tasks {} to agent {}, {}"
                                .format([task['task_id'] for task in tasks], agent_name, agent_uuid))
        current_app.logger.debug("Task details: {}".format(tasks))

    response = jsonify(tasks=tasks, return_time=return_time)

    # commit only after serializing the response
    db.session.commit()

    return response