Beispiel #1
0
    def __init__(self, trigger, params, sharedDictionary):
        Process.__init__(self)

        self.daemon = True

        self.trigger = trigger
        self.isMessageHub = params["isMessageHub"]
        self.triggerURL = self.__triggerURL(params["triggerURL"])
        self.brokers = params["brokers"]
        self.topic = params["topic"]

        self.sharedDictionary = sharedDictionary

        if 'status' in params and params['status']['active'] == False:
            self.sharedDictionary['currentState'] = Consumer.State.Disabled
            self.sharedDictionary['desiredState'] = Consumer.State.Disabled
        else:
            self.sharedDictionary['currentState'] = Consumer.State.Initializing
            self.sharedDictionary['desiredState'] = Consumer.State.Running

        if self.isMessageHub:
            self.username = params["username"]
            self.password = params["password"]

        if 'isIamKey' in params and params['isIamKey'] == True:
            self.authHandler = IAMAuth(params['authKey'], params['iamUrl'])
        else:
            if 'authKey' in params:
                auth = params['authKey'].split(':')
                self.authHandler = HTTPBasicAuth(auth[0], auth[1])
            else:
                parsedUrl = urlparse(params["triggerURL"])
                self.authHandler = HTTPBasicAuth(parsedUrl.username,
                                                 parsedUrl.password)

        # handle the case where there may be existing triggers that do not
        # have the isJSONData field set
        if "isJSONData" in params:
            self.encodeValueAsJSON = params["isJSONData"]
        else:
            self.encodeValueAsJSON = False

        if "isBinaryValue" in params:
            self.encodeValueAsBase64 = params["isBinaryValue"]
        else:
            self.encodeValueAsBase64 = False

        if "isBinaryKey" in params:
            self.encodeKeyAsBase64 = params["isBinaryKey"]
        else:
            self.encodeKeyAsBase64 = False

        # always init consumer to None in case the consumer needs to shut down
        # before the KafkaConsumer is fully initialized/assigned
        self.consumer = None

        # potentially squirrel away the message that would overflow the payload
        self.queuedMessage = None
Beispiel #2
0
    def __init__(self, trigger, params, sharedDictionary):
        Process.__init__(self)

        self.daemon = True

        self.trigger = trigger
        self.isMessageHub = params["isMessageHub"]
        self.triggerURL = self.__triggerURL(params["triggerURL"])
        self.brokers = params["brokers"]
        self.topic = params["topic"]

        self.sharedDictionary = sharedDictionary

        if 'status' in params and params['status']['active'] == False:
            self.sharedDictionary['currentState'] = Consumer.State.Disabled
            self.sharedDictionary['desiredState'] = Consumer.State.Disabled
        else:
            self.sharedDictionary['currentState'] = Consumer.State.Initializing
            self.sharedDictionary['desiredState'] = Consumer.State.Running

        if self.isMessageHub:
            self.username = params["username"]
            self.password = keyDecrypt(params["password"], trigger, True)
            self.kafkaAdminUrl = params['kafka_admin_url']

        if 'isIamKey' in params and params['isIamKey'] == True:
            decryptedKey = keyDecrypt(params['authKey'], trigger, False)
            self.authHandler = IAMAuth(decryptedKey, params['iamUrl'])
            self.isIAMTrigger = True
        else:
            self.isIAMTrigger = False
            if 'authKey' in params:
                decryptedAuthKey = keyDecrypt(params['authKey'], trigger,
                                              False)
                auth = decryptedAuthKey.split(':')
                self.authHandler = HTTPBasicAuth(auth[0], auth[1])
            else:
                parsedUrl = urlparse(params["triggerURL"])
                self.authHandler = HTTPBasicAuth(parsedUrl.username,
                                                 parsedUrl.password)

        # handle the case where there may be existing triggers that do not
        # have the isJSONData field set
        if "isJSONData" in params:
            self.encodeValueAsJSON = params["isJSONData"]
        else:
            self.encodeValueAsJSON = False

        if "isBinaryValue" in params:
            self.encodeValueAsBase64 = params["isBinaryValue"]
        else:
            self.encodeValueAsBase64 = False

        if "isBinaryKey" in params:
            self.encodeKeyAsBase64 = params["isBinaryKey"]
        else:
            self.encodeKeyAsBase64 = False

        try:
            response = requests.get(self.triggerURL,
                                    auth=self.authHandler,
                                    timeout=10.0,
                                    verify=check_ssl)
            status_code = response.status_code
            msg = "[{}] At consumer start up. Repsonse status code {}".format(
                self.trigger, status_code)
            logging.info(msg)
            #if self.__shouldDisableDuringConsumerStartUp(status_code):
            #self.__disableTrigger(status_code, msg)
            #self.__recordState(self.desiredState())
        except requests.exceptions.RequestException as e:
            logging.error('[{}] Error getting trigger: {}'.format(
                self.trigger, e))
        except AuthHandlerException as e:
            msg = '[{}] At consumer start up. Encountered an exception from auth handler, status code {}'.format(
                self.trigger, e.response.status_code)
            logging.error(msg)
            # if self.__shouldDisableDuringConsumerStartUp(e.response.status_code):
            #     self.__disableTrigger(e.response.status_code, msg)
            #     self.__recordState(self.desiredState())

        # always init consumer to None in case the consumer needs to shut down
        # before the KafkaConsumer is fully initialized/assigned
        self.consumer = None

        # potentially squirrel away the message that would overflow the payload
        self.queuedMessage = None