def run(self): # Establish a connection with the redis server r = redis.Redis(host=self.host,port=self.port,db=self.db) tasklib.register("resolver",self) try: while True: # Get an outgoing message target, msg = self.recv() print("RESOLVER:", target, msg) # Check if a proxy was already registered if tasklib.lookup(target): tasklib.send(target,msg) continue # See if redis knows anything about the target try: target_info = r.get(target) if target_info: host, port = target_info.decode('utf-8').split(":") port = int(port) # Create a proxy task proxy(target,target,(host,port),self.authkey) self.log.info("Connected to %s", (host,port)) # Send the message to the proxy tasklib.send(target,msg) else: self.log.info("Nothing known about target '%s'", target) except Exception as e: self.log.info("Couldn't resolve '%s' : %s:%s", target, type(e),e) finally: tasklib.unregister("resolver")
def proxy(proxyname,target,address,authkey): conn = Client(address,authkey=authkey) pxy = ProxyTask(proxyname,target,conn) pxy.start() tasklib.register(proxyname,pxy)
self.targets = targets def run(self): while True: msg = self.recv() for target in self.targets: tasklib.send(target, msg) if __name__ == "__main__": import time import logging logging.basicConfig(level=logging.INFO) # Start my message dispatcher taskdist.start_dispatcher(("localhost", 16000), authkey=b"peekaboo") # Register proxies to the different servers taskdist.proxy("busdb-1", "busdb", ("localhost", 17000), authkey=b"peekaboo") taskdist.proxy("busdb-2", "busdb", ("localhost", 17001), authkey=b"peekaboo") # Start the message broker broker_task = BusDbBrokerTask(["busdb-1", "busdb-2"]) broker_task.start() tasklib.register("busdb", broker_task) # Spin while True: time.sleep(1)
while True: tag, msg = self.recv() # If 'bus' message, update the internal dictionary if tag == 'bus': bus = msg self.data[bus['id']] = bus # If 'getid' message, get the busid and target for response data elif tag == 'getid': busid, target = msg bus = self.data.get(busid) time.sleep(1) tasklib.send(target, ('bus',copy.deepcopy(bus))) if __name__ == '__main__': import taskdist import logging import time logging.basicConfig(level=logging.INFO) # Start the dispatcher taskdist.start_dispatcher(("localhost",18000),authkey=b"peekaboo") taskdist.start_resolver(authkey=b"peekaboo") busdb_task = BusDbTask() busdb_task.start() tasklib.register("busdb",busdb_task,export=True) while True: time.sleep(1)
while True: msg = self.recv() for target in self.targets: tasklib.send(target, msg) if __name__ == '__main__': import time import logging logging.basicConfig(level=logging.INFO) # Start my message dispatcher taskdist.start_dispatcher(("localhost", 16000), authkey=b"peekaboo") # Register proxies to the different servers taskdist.proxy("busdb-1", "busdb", ("localhost", 17000), authkey=b"peekaboo") taskdist.proxy("busdb-2", "busdb", ("localhost", 17001), authkey=b"peekaboo") # Start the message broker broker_task = BusDbBrokerTask(["busdb-1", "busdb-2"]) broker_task.start() tasklib.register("busdb", broker_task) # Spin while True: time.sleep(1)