def subscribe(self, topic: str): """ Actually sends the subscription request, by picking a broker at random """ logging.info("Trying to discover brokers in the swarm") brokers = discover(get_current_host_ip(), service_type=SERVICE_TYPE.TOPIC_BROKER) # Choose a random broker subscribe_to = random.choice(brokers) poll_ctr = 0 while not poll(subscribe_to) and poll_ctr < 5: subscribe_to = random.choice(Config.DOCKER_BROKER_ADDRESSES) poll_ctr += 1 logging.info("Subscribing to Host: {0}".format(subscribe_to)) if not poll(subscribe_to): logging.error("Broker {0} is down".format(subscribe_to)) return False # The topic to subscribe to subscribe_topic = topic if topic else self.topic # Format the url by adding the schema, eg. 'http://' to it url = add_schema_to_url("{0}/{1}/{2}".format(subscribe_to, ENDPOINTS.SUBSCRIBE, subscribe_topic)) logging.info("Calling URL {}".format(url)) response = requests.get(url) response_json = response.json() if response_json['brokers']: self.brokers = response_json['brokers'] logging.info("Active brokers: {0}".format(self.brokers)) if response.status_code == 200: self.subscriptions[subscribe_topic] = True else: self.subscriptions[subscribe_topic] = False return self.subscriptions[subscribe_topic]
def get_next(): file_path = utils.poll(real_path('processed_votes')) lines_processed = utils.lines(real_path('processed_votes')) lines_processing = utils.lines(real_path('processing_votes')) to_process = PROCESSING_BUFFER_SIZE - lines_processed - lines_processing process(to_process) return file_path
def notify(self, subscriber, topic, message): """ Sends the message to a subscriber """ if not poll(subscriber): logging.error("Subscriber {0} is down".format(subscriber)) return False url = "{0}/{1}".format(subscriber, ENDPOINTS.NOTIFY) cleaned_url = add_schema_to_url(url) logging.info("Notifying URL: {}".format(cleaned_url)) post_data = {'topic': topic, 'data': message} logging.info("Message being sent is: {}".format(message)) response = requests.post(cleaned_url, data=post_data) return response.status_code == 200
def publish(self, message): """ Publish message to the central broker """ publish_to = Config.BROKER_ADDRESS # Check if broker is up if not poll(publish_to): logging.error("Broker {0} is down".format(publish_to)) return False url = add_schema_to_url("{0}/{1}/{2}".format(publish_to, ENDPOINTS.PUBLISH, self.topic)) logging.info("Publishing to URL {0} : {1}".format(url, message)) resp = requests.post(url, data=message) return resp
def subscribe(self, topic: str): """ Subscribe to a topic at the central broker """ if not poll(SUBSCRIBE_TO): logging.error("Broker {0} is down".format(SUBSCRIBE_TO)) return False subscribe_topic = topic if topic else self.topic url = add_schema_to_url("{0}/{1}/{2}".format(SUBSCRIBE_TO, ENDPOINTS.SUBSCRIBE, subscribe_topic)) logging.info("Calling URL {}".format(url)) response = requests.get(url) if response.status_code == 200: self.subscriptions[subscribe_topic] = True else: self.subscriptions[subscribe_topic] = False return self.subscriptions[subscribe_topic]