def main_loop(self): """Run the main collector loop Every message recieved will result in ``message_recieved`` being called with the de-serialized JSON data. Every 10 seconds, the ``flush`` method will be called for storage instances that wish to flush collected messages periodically for efficiency. ``flush`` will *only* be called if there actually were messages in the prior 10 seconds. The main_loop executes in a serial single-threaded fashion. """ print "Running zilch-recorder on port: %s" % self.zeromq_bind messages = False now = time.time() last_flush = now while 1: try: message = self.sock.recv(flags=zmq.NOBLOCK) data = loads(message.decode('zlib')) self.store.message_received(data) messages = True except zmq.ZMQError, e: if e.errno != zmq.EAGAIN: raise time.sleep(0.2) now = time.time() if now - last_flush > 5 and messages: self.store.flush() last_flush = now messages = False
def shutdown(self, signum, stack): """Shutdown the main loop and handle remaining messages""" self.sock.close() messages = True message_count = 0 while messages: try: message = self.sock.recv(flags=zmq.NOBLOCK) data = loads(message.decode('zlib')) self.store.message_received(data) message_count += 1 except zmq.ZMQError, e: messages = False