def run(self, in_addr="0.0.0.0:30002", out_addr="tcp://*:5000"): self.is_running = True signal.signal(signal.SIGTERM, self.stop) ip, port = in_addr.split(':') self.child = Decoder(os.getpid(), out_addr) self.child.start() time.sleep(.5) logger.info("Listen on %s:%s, output goes to: %s" % (ip, port, out_addr)) context = zmq.Context() self.push = context.socket(zmq.PUSH) self.push.connect("ipc:///tmp/pinba2zmq.sock") pool = Pool(5000) self.server = DgramServer(ip, int(port), self.recv, spawn=pool) logger.info("Ready!") try: gevent.spawn(self.watcher) self.workers = [gevent.spawn_later(1, self.interval)] self.server.serve_forever() except KeyboardInterrupt: pass except Exception: logger.error(traceback.format_exc()) logger.info("Daemon shutting down") self.is_running = False gevent.killall(self.workers) self.pub.close() self.child.terminate() logger.info("Daemon stops")
class PinbaToZmq(object): """ Pinba Daemon - starts UDP server on port 30002, recives packets from php, every second sends them to decoder, and then sends them in usable format via zmq socket. """ def __init__(self): self.requests = [] self.is_running = False self.pub = None self.req = None def recv(self, msg, address): # this handler will be run for each incoming connection in a dedicated greenlet self.requests.append(msg) def interval(self): # every second collect requests from self.requests and send them to processing if self.is_running: gevent.spawn_later(1, self.interval) self.push.send_pyobj((int(time.time()), self.requests)) self.requests = [] def stop(self, signum=None, frame=None): self.is_running = False def watcher(self): try: while self.is_running: gevent.sleep(1) logger.info("Try to stop server...") self.server.stop() except Exception: logger.error(traceback.format_exc()) logger.info("Watcher thread stops") def run(self, in_addr="0.0.0.0:30002", out_addr="tcp://*:5000"): self.is_running = True signal.signal(signal.SIGTERM, self.stop) ip, port = in_addr.split(':') self.child = Decoder(os.getpid(), out_addr) self.child.start() time.sleep(.5) logger.info("Listen on %s:%s, output goes to: %s" % (ip, port, out_addr)) context = zmq.Context() self.push = context.socket(zmq.PUSH) self.push.connect("ipc:///tmp/pinba2zmq.sock") pool = Pool(5000) self.server = DgramServer(ip, int(port), self.recv, spawn=pool) logger.info("Ready!") try: gevent.spawn(self.watcher) self.workers = [gevent.spawn_later(1, self.interval)] self.server.serve_forever() except KeyboardInterrupt: pass except Exception: logger.error(traceback.format_exc()) logger.info("Daemon shutting down") self.is_running = False gevent.killall(self.workers) self.pub.close() self.child.terminate() logger.info("Daemon stops")