def failover(mb_urls, mb_client): """ Iterate through the list of message brokers provided and connect to the first available server. This will not return until a message broker connection is established. :param mb_urls: the list of message broker URLS of format [host:port, host:port] :param mb_client: the initialized message broker client object :return: a tuple of the connected message broker client, connected message broker IP address and connected message broker port """ # Connection retry interval incrementer message_broker_retry_timer = IncrementalCeilingListIterator( [2, 2, 5, 5, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60], False) # Cycling through the provided mb urls until forever while True: retry_interval = message_broker_retry_timer.get_next_retry_interval() for mb_url in mb_urls: mb_ip, mb_port = mb_url.split(":") EventSubscriber.log.debug( "Trying to connect to the message broker with address %r:%r" % (mb_ip, mb_port)) try: mb_client.connect(mb_ip, mb_port, 60) return mb_client, mb_ip, mb_port except: # The message broker didn't respond well EventSubscriber.log.debug("Could not connect to the message broker at %s:%s." % (mb_ip, mb_port)) EventSubscriber.log.debug( "Could not connect to any of the message brokers provided. Retrying in %s seconds." % retry_interval) time.sleep(retry_interval)
def failover(mb_urls, mb_client): """ Iterate through the list of message brokers provided and connect to the first available server. This will not return until a message broker connection is established. :param mb_urls: the list of message broker URLS of format [host:port, host:port] :param mb_client: the initialized message broker client object :return: a tuple of the connected message broker client, connected message broker IP address and connected message broker port """ # Connection retry interval incrementer message_broker_retry_timer = IncrementalCeilingListIterator( [2, 2, 5, 5, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60], False) # Cycling through the provided mb urls until forever while True: retry_interval = message_broker_retry_timer.get_next_retry_interval() for mb_url in mb_urls: mb_ip, mb_port = mb_url.split(":") EventSubscriber.log.debug( "Trying to connect to the message broker with address %r:%r" % (mb_ip, mb_port)) try: mb_client.connect(mb_ip, mb_port, 60) return mb_client, mb_ip, mb_port except: # The message broker didn't respond well EventSubscriber.log.info("Could not connect to the message broker at %s:%s." % (mb_ip, mb_port)) EventSubscriber.log.error( "Could not connect to any of the message brokers provided. Retrying in %s seconds." % retry_interval) time.sleep(retry_interval)
def publish(self, event): if Config.mb_username is None: auth = None else: auth = {"username": Config.mb_username, "password": Config.mb_password} payload = event.to_json() retry_iterator = IncrementalCeilingListIterator([2, 2, 5, 5, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60], False) # Retry to publish the event until the timeout exceeds while int(time.time()) - self.__start_time < (Config.mb_publisher_timeout * 1000): retry_interval = retry_iterator.get_next_retry_interval() for mb_url in Config.mb_urls.split(","): mb_ip, mb_port = mb_url.split(":") # start a thread to execute publish event publisher_thread = Thread(target=self.__publish_event, args=(event, mb_ip, mb_port, auth, payload)) publisher_thread.setDaemon(True) publisher_thread.setName("MBEventPublisherThreadForEvent%s" % event.__class__.__name__) self.__log.debug("Starting a publisher thread for event %s " % event.__class__.__name__) publisher_thread.start() # give sometime for the thread to complete time.sleep(5) # check if thread is still running and notify if publisher_thread.isAlive(): self.__log.debug( "Event publishing timed out before succeeding. The message broker could be offline." ) # check if publish.single() succeeded try: published = self.__msg_queue.get(block=False) except Empty: published = False if published: return True # All the brokers on the list were offline self.__log.debug( "Could not publish event to any of the provided message brokers. Retrying in %s seconds." % retry_interval ) time.sleep(retry_interval) # Even publisher timeout exceeded self.__log.warn( "Could not publish event to any of the provided message brokers before " "the timeout [%s] exceeded. The event will be dropped." % Config.mb_publisher_timeout ) return False
def publish(self, event): if Config.mb_username is None: auth = None else: auth = {"username": Config.mb_username, "password": Config.mb_password} payload = event.to_json() retry_iterator = IncrementalCeilingListIterator([2, 2, 5, 5, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60], False) # Retry to publish the event until the timeout exceeds while int(time.time()) - self.__start_time < (Config.mb_publisher_timeout * 1000): retry_interval = retry_iterator.get_next_retry_interval() for mb_url in Config.mb_urls.split(","): mb_ip, mb_port = mb_url.split(":") # start a thread to execute publish event publisher_thread = Thread(target=self.__publish_event, args=(event, mb_ip, mb_port, auth, payload)) publisher_thread.start() # give sometime for the thread to complete time.sleep(5) # check if thread is still running and notify if publisher_thread.isAlive(): self.__log.debug( "Event publishing timed out before succeeding. The message broker could be offline.") # check if publish.single() succeeded try: published = self.__msg_queue.get(block=False) except Empty: published = False if published: return True # All the brokers on the list were offline self.__log.debug( "Could not publish event to any of the provided message brokers. Retrying in %s seconds." % retry_interval) time.sleep(retry_interval) # Even publisher timeout exceeded self.__log.warn("Could not publish event to any of the provided message brokers before " "the timeout [%s] exceeded. The event will be dropped." % Config.mb_publisher_timeout) return False