def __init__(self, frontend=DEFAULT_FRONTEND, backend=DEFAULT_BACKEND, heartbeat=DEFAULT_HEARTBEAT, register=DEFAULT_REG, io_threads=DEFAULT_IOTHREADS, worker_timeout=DEFAULT_TIMEOUT_MOVF): # before doing anything, we verify if a broker is already up and # running logger.debug('Verifying if there is a running broker') pid = verify_broker(frontend) if pid is not None: # oops. can't do this ! logger.debug('Ooops, we have a running broker on that socket') raise DuplicateBrokerError(pid) logger.debug('Initializing the broker.') for endpoint in (frontend, backend, heartbeat): if endpoint.startswith('ipc'): register_ipc_file(endpoint) self.context = zmq.Context(io_threads=io_threads) # setting up the three sockets self._frontend = self.context.socket(zmq.ROUTER) self._frontend.identity = 'broker-' + frontend self._frontend.bind(frontend) self._backend = self.context.socket(zmq.ROUTER) self._backend.bind(backend) self._registration = self.context.socket(zmq.PULL) self._registration.bind(register) # setting up the streams self.loop = ioloop.IOLoop() self._frontstream = zmqstream.ZMQStream(self._frontend, self.loop) self._frontstream.on_recv(self._handle_recv_front) self._backstream = zmqstream.ZMQStream(self._backend, self.loop) self._backstream.on_recv(self._handle_recv_back) self._regstream = zmqstream.ZMQStream(self._registration, self.loop) self._regstream.on_recv(self._handle_reg) # heartbeat self.pong = Heartbeat(heartbeat, io_loop=self.loop, ctx=self.context) # status self.started = False self.poll_timeout = None # workers registration and timers self._workers = [] self._worker_times = {} self.worker_timeout = worker_timeout
def __init__(self, frontend=DEFAULT_FRONTEND, backend=DEFAULT_BACKEND, heartbeat=DEFAULT_HEARTBEAT): # before doing anything, we verify if a broker is already up and # running logger.debug('Verifying if there is a running broker') pid = verify_broker(frontend) if pid is not None: # oops. can't do this ! logger.debug('Ooops, we have a running broker on that socket') raise DuplicateBrokerError(pid) logger.debug('Initializing the broker.') for endpoint in (frontend, backend, heartbeat): if endpoint.startswith('ipc'): register_ipc_file(endpoint) self.context = zmq.Context() # setting up the two sockets self._frontend = self.context.socket(zmq.ROUTER) self._frontend.identity = 'broker-' + frontend self._frontend.bind(frontend) self._backend = self.context.socket(zmq.DEALER) self._backend.bind(backend) # setting up the poller self.loop = ioloop.IOLoop() self._frontstream = zmqstream.ZMQStream(self._frontend, self.loop) self._frontstream.on_recv(self._handle_recv_front) self._backstream = zmqstream.ZMQStream(self._backend, self.loop) self._backstream.on_recv(self._handle_recv_back) # heartbeat self.pong = Heartbeat(heartbeat, io_loop=self.loop) # status self.started = False self.poll_timeout = None
def main(args=sys.argv): parser = argparse.ArgumentParser(description='Powerhose broker.') parser.add_argument('--frontend', dest='frontend', default=DEFAULT_FRONTEND, help="ZMQ socket to receive jobs.") parser.add_argument('--backend', dest='backend', default=DEFAULT_BACKEND, help="ZMQ socket for workers.") parser.add_argument('--heartbeat', dest='heartbeat', default=DEFAULT_HEARTBEAT, help="ZMQ socket for the heartbeat.") parser.add_argument('--debug', action='store_true', default=False, help="Debug mode") parser.add_argument('--check', action='store_true', default=False, help=("Use this option to check if there's a running " " broker. Returns the PID if a broker is up.")) parser.add_argument('--purge-ghosts', action='store_true', default=False, help="Use this option to purge ghost brokers.") parser.add_argument('--logfile', dest='logfile', default='stdout', help="File to log in to .") args = parser.parse_args() set_logger(args.debug, logfile=args.logfile) if args.purge_ghosts: broker_pids, ghosts = kill_ghost_brokers(args.frontend) if broker_pids is None: logger.info('No running broker.') else: logger.info('The active broker runs at PID: %s' % broker_pids) if len(ghosts) == 0: logger.info('No ghosts where killed.') else: logger.info('Ghost(s) killed: %s' \ % ', '.join([str(g) for g in ghosts])) return 0 if args.check: pid = verify_broker(args.frontend) if pid is None: logger.info('There seem to be no broker on this endpoint') else: logger.info('A broker is running. PID: %s' % pid) return 0 logger.info('Starting the broker') try: broker = Broker(frontend=args.frontend, backend=args.backend, heartbeat=args.heartbeat) except DuplicateBrokerError, e: logger.info('There is already a broker running on PID %s' % e) logger.info('Exiting') return 1
def main(args=sys.argv): parser = argparse.ArgumentParser(description='Powerhose broker.') parser.add_argument('--frontend', dest='frontend', default=DEFAULT_FRONTEND, help="ZMQ socket to receive jobs.") parser.add_argument('--backend', dest='backend', default=DEFAULT_BACKEND, help="ZMQ socket for workers.") parser.add_argument('--heartbeat', dest='heartbeat', default=DEFAULT_HEARTBEAT, help="ZMQ socket for the heartbeat.") parser.add_argument('--register', dest='register', default=DEFAULT_REG, help="ZMQ socket for the registration.") parser.add_argument('--io-threads', type=int, default=DEFAULT_IOTHREADS, help="Number of I/O threads") parser.add_argument('--debug', action='store_true', default=False, help="Debug mode") parser.add_argument('--check', action='store_true', default=False, help=("Use this option to check if there's a running " " broker. Returns the PID if a broker is up.")) parser.add_argument('--purge-ghosts', action='store_true', default=False, help="Use this option to purge ghost brokers.") parser.add_argument('--logfile', dest='logfile', default='stdout', help="File to log in to .") args = parser.parse_args() set_logger(args.debug, logfile=args.logfile) if args.purge_ghosts: broker_pids, ghosts = kill_ghost_brokers(args.frontend) if broker_pids is None: logger.info('No running broker.') else: logger.info('The active broker runs at PID: %s' % broker_pids) if len(ghosts) == 0: logger.info('No ghosts where killed.') else: logger.info('Ghost(s) killed: %s' \ % ', '.join([str(g) for g in ghosts])) return 0 if args.check: pid = verify_broker(args.frontend) if pid is None: logger.info('There seem to be no broker on this endpoint') else: logger.info('A broker is running. PID: %s' % pid) return 0 logger.info('Starting the broker') try: broker = Broker(frontend=args.frontend, backend=args.backend, heartbeat=args.heartbeat, register=args.register, io_threads=args.io_threads) except DuplicateBrokerError, e: logger.info('There is already a broker running on PID %s' % e) logger.info('Exiting') return 1