Esempio n. 1
0
async def handler(context, event):

    context.logger.debug(
        'Received event! event.body: {0}, event.headers: {1}'.format(
            event.body, event.headers))

    if event.trigger.kind in ('kafka-cluster', 'v3ioStream', 'v3io-stream'):

        context.logger.debug(
            'Adding event to offset queue - event.body: {0}, event.offset: {1}'
            .format(
                event.body,
                event.offset,
            ))

        # add event to file
        write_event_offset_to_file(context, event)

        # ensure no ack on response
        response = nuclio_sdk.Response()
        response.status_code = 200
        response.ensure_no_ack()

        return response

    if event.trigger.kind == 'http':

        response = nuclio_sdk.Response()
        response.status_code = 200
        response_body = {}

        resource = json.loads(event.body).get('resource')

        if resource == 'queue_size':
            queue_size = get_number_of_enqueued_events()
            context.logger.info('current queue size: {0}'.format(queue_size))
            response_body['queue_size'] = queue_size

        elif resource == 'last_committed_offset':
            last_commit_offset = get_last_commit_offset()
            context.logger.info(
                'last committed offset: {0}'.format(last_commit_offset))
            response_body['last_committed_offset'] = last_commit_offset

        elif resource == 'start_processing':
            context.logger.info('start processing')
            await process_events(context)
            response_body['started_processing'] = True

        if not resource:
            response_body['not_implemented'] = True
            response.status_code = 400

        response.body = response_body
        return response
Esempio n. 2
0
    def call_function(self, function_name, event, node=None, timeout=None):

        # get connection from provider
        connection = self._connection_provider(
            self._get_function_url(function_name), timeout=timeout)

        # if the user passes a dict as a body, assume json serialization. otherwise take content type from
        # body or use plain text
        if isinstance(event.body, dict):
            body = json.dumps(event.body)
            content_type = 'application/json'
        else:
            body = event.body
            content_type = event.content_type or 'text/plain'

        # use the headers from the event or default to empty dict
        headers = event.headers or {}
        headers['Content-Type'] = content_type

        # if no override header, use the name of the function to indicate the target
        # this is needed to cold start a function in case it was scaled to zero
        headers['X-Nuclio-Target'] = event.headers.get('X-Nuclio-Target',
                                                       function_name)

        # let http client determine that
        headers.pop('Content-Length', None)

        try:
            connection.request(event.method,
                               event.path,
                               body=body,
                               headers=headers)

            # get response from connection
            response = connection.getresponse()

            # read the body
            response_body = response.read()
        finally:
            connection.close()

        # header dict
        response_headers = {}

        # get response headers as lowercase
        for (name, value) in response.getheaders():
            response_headers[name.lower()] = value

        # if content type exists, use it
        response_content_type = response_headers.get('content-type',
                                                     'text/plain')

        # if content type is json, go ahead and do parsing here. if it explodes, don't blow up
        if response_content_type == 'application/json':
            response_body = json.loads(response_body)

        return nuclio_sdk.Response(headers=response_headers,
                                   body=response_body,
                                   content_type=response_content_type,
                                   status_code=response.status)
Esempio n. 3
0
    def call_function(self, function_name, event, node=None, timeout=None):

        # get connection from provider
        connection = self._connection_provider(self._get_function_url(function_name), timeout=timeout)

        # if the user passes a dict as a body, assume json serialization. otherwise take content type from
        # body or use plain text
        if isinstance(event.body, dict):
            body = json.dumps(event.body)
            content_type = 'application/json'
        else:
            body = event.body
            content_type = event.content_type or 'text/plain'

        # use the headers from the event or default to empty dict
        headers = event.headers or {}
        headers['Content-Type'] = content_type

        connection.request(event.method,
                           event.path,
                           body=body,
                           headers=headers)

        # get response from connection
        connection_response = connection.getresponse()

        # header dict
        response_headers = {}

        # get response headers as lowercase
        for (name, value) in connection_response.getheaders():
            response_headers[name.lower()] = value

        # if content type exists, use it
        response_content_type = response_headers.get('content-type', 'text/plain')

        # read the body
        response_body = connection_response.read()

        # if content type is json, go ahead and do parsing here. if it explodes, don't blow up
        if response_content_type == 'application/json':
            response_body = json.loads(response_body)

        response = nuclio_sdk.Response(headers=response_headers,
                                       body=response_body,
                                       content_type=response_content_type,
                                       status_code=connection_response.status)

        return response