def open_socket(self): self.context = zmq.Context() self.socket = self.context.socket(zmq.PULL) if self.identity: self.socket.setsockopt(zmq.IDENTITY, self.identity) if self.bind: log.debug('%s: Binding socket to %s' % (self, self.endpoint)) self.socket.bind(self.endpoint) else: log.debug('%s: Connecting socket to %s' % (self, self.endpoint)) self.socket.connect(self.endpoint)
def __init__(self, name, endpoint, identity=None, bind=False): Sink.__init__(self, name) self.endpoint = endpoint self.identity = identity self.context = zmq.Context() self.socket = self.context.socket(zmq.PUSH) if bind: log.debug('%s: Binding socket to %s' % (self, endpoint)) self.socket.bind(endpoint) else: log.debug('%s: Connecting socket to %s' % (self, endpoint)) self.socket.connect(endpoint)
def run(self): log.debug('%s: Spawning %s' % (self, self.command)) self.proc = subprocess.Popen(self.command, shell=True, bufsize=0, stdout=subprocess.PIPE) while True: line = self.proc.stdout.readline() if not line: break self.process(self.key, int(time.time()), line.rstrip('\r\n ')) ret = self.proc.wait() if ret != 0: log.warning('%s: Exited with return code %i' % (self, ret)) else: log.info('%s: Exited with return code %i' % (self, ret))
except zmq.ZMQError, e: break if len(m) != 3: log.warning('%s: Wrong number of parts in message: %r' % \ (self, m)) continue else: key, ts, message = m try: ts = int(ts) except ValueError, e: log.warning('%s: Malformed line: %s' % (self, e)) continue self.process(key, int(ts), message) log.debug('%s: Receive loop stopped' % self) class ZMQPushSink(Sink): def __init__(self, name, endpoint, identity=None, bind=False): Sink.__init__(self, name) self.endpoint = endpoint self.identity = identity self.context = zmq.Context() self.socket = self.context.socket(zmq.PUSH) if bind: log.debug('%s: Binding socket to %s' % (self, endpoint)) self.socket.bind(endpoint) else: log.debug('%s: Connecting socket to %s' % (self, endpoint)) self.socket.connect(endpoint)
def reload(self): if self.proc is None: return log.debug('%s: Killing pid %i' % (self, self.proc.pid)) self.stop() eventlet.spawn_n(self.run)