def run(self): interval = constants.INTERVAL_INIT while True: self._run_handler() if not self.auto_recovery: raise Exception else: log.warning("Heartbeat failure, can't reach queue") log.info("Reconnecting in %0.2fs…", interval) sleep(interval) if interval < constants.INTERVAL_MAX: interval *= 2 return True
def disconnect(self, terminate=False): if not self.context: raise Exception("Client has been terminated.") if self.client: log.info("Disconnecting from server…") self.client.setsockopt(zmq.LINGER, 0) self.client.close() self.poll.unregister(self.client) self.client = None if terminate: self.context.term() self.context = None log.info("Client terminated.")
def connect(self): if self.client: log.error("Client already connected to server.") return log.info("Connecting to server…") self.client = self.context.socket(zmq.REQ) identity = b"%04X-%04X" % (randint(0, 0x10000), randint(0, 0x10000)) log.info(str(identity)) self.client.setsockopt(zmq.IDENTITY, identity) self.client.connect(self.server_address) self.poll.register(self.client, zmq.POLLIN)
def _start_routing(self): self.frontend.bind(self.frontend_address) # For clients self.backend.bind(self.backend_address) # For workers poll_workers = zmq.Poller() poll_workers.register(self.backend, zmq.POLLIN) poll_both = zmq.Poller() poll_both.register(self.frontend, zmq.POLLIN) poll_both.register(self.backend, zmq.POLLIN) self.heartbeat_at = time() + constants.HEARTBEAT_INTERVAL while True: if not self._message_loop(poll_workers, poll_both): return log.info("Finishing message broker.")
def run(self): log.info("Starting messaging broker: frontend at %s, backend at %s", self.frontend_address, self.backend_address) self._create_routers() self._start_routing()