def emit_info(self, channel, info, wrap_in_list): # info can be a list (e.g. for leases) # or a dict, with or without an 'id' field if self.debug: msg = "len={}".format(len(info)) if isinstance(info, list) \ else "id={}".format(info.get('id', '[none]')) logger.info("{} emitting {} -> {}" .format(channel, msg, info)) # wrap info as single elt in a list if wrap_in_list: info = [ info ] message = json.dumps(info) if self.socketio is None: self.connect() try: self.socketio.emit(channel, message, ReconnectableSocketIO.callback) self.counters.setdefault(channel, 0) self.counters[channel] += 1 except Exception as e: # make sure we reconnect later on self.socketio = None label = "{}: {}".format(info.get('id', 'none'), one_char_summary(info)) logger.warn("Dropped message on {} - channel {} - msg {}" .format(self, channel, label)) logger.exception("Dropped because of this exception")
def connect(self): action = "connect" if self.socketio is None else "reconnect" try: logger.info("{}ing to {}".format(action, self)) self.socketio = SocketIO(self.hostname, self.port, LoggingNamespace) channel = back_channel def closure(*args): return self.on_channel(channel, *args) self.socketio.on(channel, closure) except: logger.warn("Connection lost to {}".format(self)) self.socketio = None