Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Do not start a consumer for a disabled trigger #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions provider/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ def __init__(self, trigger, params, sharedDictionary):
if self.isMessageHub:
self.username = params["username"]
self.password = params["password"]
self.kafkaAdminUrl = params['kafka_admin_url']

try:
auth = self.password if self.username.lower() == 'token' else self.username + self.password
response = requests.get(self.kafkaAdminUrl, headers={'X-Auth-Token': auth}, timeout=60.0, verify=check_ssl)

if response.status_code == 403:
logging.info("[{}] Invalid Kafka auth, disabling trigger... {}".format(self.trigger, response.status_code))
self.__disableTrigger(response.status_code, 'Automatically disabled due to invalid Kafka credentials.')
except requests.exceptions.RequestException as e:
logging.info("[{}] Exception during Kafka auth, continuing... {}".format(self.trigger, e))

if 'isIamKey' in params and params['isIamKey'] == True:
self.authHandler = IAMAuth(params['authKey'], params['iamUrl'])
Expand Down Expand Up @@ -439,13 +450,13 @@ def __fireTrigger(self, messages):
self.consumer.commit(offsets=self.__getOffsetList(messages), async=False)
retry = False

def __disableTrigger(self, status_code):
def __disableTrigger(self, status_code, message='Automatically disabled after receiving a {} status code when firing the trigger.'):
self.setDesiredState(Consumer.State.Disabled)

# when failing to establish a database connection, mark the consumer as dead to restart the consumer
try:
self.database = Database()
self.database.disableTrigger(self.trigger, status_code)
self.database.disableTrigger(self.trigger, status_code, message)
except Exception as e:
logging.error('[{}] Uncaught exception: {}'.format(self.trigger, e))
self.__recordState(Consumer.State.Dead)
Expand Down Expand Up @@ -566,7 +577,7 @@ def __on_assign(self, consumer, partitions):
logging.info('[{}] Completed partition assignment. Connected to broker(s)'.format(self.trigger))

if self.currentState() == Consumer.State.Initializing and self.__shouldRun():
logging.info('[{}] Setting consumer state to runnning.'.format(self.trigger))
logging.info('[{}] Setting consumer state to running.'.format(self.trigger))
self.__recordState(Consumer.State.Running)

def __on_revoke(self, consumer, partitions):
Expand Down
2 changes: 1 addition & 1 deletion provider/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def destroy(self):
self.client.disconnect()
self.client = None

def disableTrigger(self, triggerFQN, status_code, message='Automatically disabled after receiving a {} status code when firing the trigger.'):
def disableTrigger(self, triggerFQN, status_code, message):
try:
document = self.database[triggerFQN]

Expand Down
5 changes: 4 additions & 1 deletion provider/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __handleDocChange(self, change):
self.createAndRunConsumer(document)
else:
logging.info("[{}] Found a new trigger, but is assigned to another worker: {}".format(change["id"], document["worker"]))

else:
existingConsumer = self.consumers.getConsumerForTrigger(change["id"])

Expand Down Expand Up @@ -181,10 +182,12 @@ def createAndRunConsumer(self, doc):
# Create a representation for this trigger, even if it is disabled
# This allows it to appear in /health as well as allow it to be deleted
# Creating this object is lightweight and does not initialize any connections

# TODO: don't want to run the process...
consumer = Consumer(triggerFQN, doc)
self.consumers.addConsumerForTrigger(triggerFQN, consumer)

if self.__isTriggerDocActive(doc):
if self.__isTriggerDocActive(doc) and consumer.desiredState() != Consumer.State.Disabled:
logging.info('[{}] Trigger was determined to be active, starting...'.format(triggerFQN))
consumer.start()
else:
Expand Down