Пример #1
0
 def start(self):
     """
     Run main server loop
     """
     parser = self.create_parser()
     self.add_arguments(parser)
     options = parser.parse_args(sys.argv[1:])
     cmd_options = vars(options)
     args = cmd_options.pop("args", ())
     # Bootstrap logging with --loglevel
     self.setup_logging(cmd_options["loglevel"])
     self.log_separator()
     # Setup timezone
     try:
         self.logger.info("Setting timezone to %s", config.timezone)
         setup_timezone()
     except ValueError as e:
         self.die(str(e))
     # Setup title
     self.set_proc_title()
     # Setup signal handlers
     self.setup_signal_handlers()
     self.on_start()
     # Starting IOLoop
     self.is_active = True
     if self.pooled:
         self.logger.warn(
             "Running service %s (pool: %s)",
             self.name, config.pool
         )
     else:
         self.logger.warn(
             "Running service %s", self.name
         )
     try:
         if config.features.use_uvlib:
             from tornaduv import UVLoop
             self.logger.warn("Using libuv")
             tornado.ioloop.IOLoop.configure(UVLoop)
         self.ioloop = tornado.ioloop.IOLoop.instance()
         # Initialize DCS
         self.dcs = get_dcs(cmd_options["dcs"], self.ioloop)
         # Activate service
         self.ioloop.add_callback(self.activate)
         self.logger.warn("Starting IOLoop")
         self.ioloop.start()
     except KeyboardInterrupt:
         self.logger.warn("Interrupted by Ctrl+C")
     except self.RegistrationError:
         self.logger.info("Registration failed")
     except Exception:
         error_report()
     finally:
         if self.ioloop:
             self.ioloop.add_callback(self.deactivate())
     for cb, args, kwargs in self.close_callbacks:
         cb(*args, **kwargs)
     self.logger.warn("Service %s has been terminated", self.name)
Пример #2
0
def mpub(topic, messages, dcs=None, io_loop=None, retries=None):
    """
    Asynchronously publish message to NSQ topic

    :param topic: NSQ topic
    :param messages: List of strings containing messages
    :param dcs: DSC instance
    :param io_loop: IOLoop instance
    :param retries: Error retries. config.nsqd.pub_retries by default
    :return: None
    :raises NSQPubError: On publish error
    """
    if not messages:
        raise tornado.gen.Return()
    if not dcs:
        # No global DCS, instantiate one
        dcs = get_dcs(ioloop=io_loop)
    # Build body
    msg = mpub_encode(messages)
    # Setup resolver
    services = nsqd_http_service_param.services
    num_services = len(services)
    if num_services > 1:
        s_index = random.randint(0, num_services - 1)
    else:
        s_index = 0
    # Post message
    retries = retries or config.nsqd.pub_retries
    code = 200
    body = None
    metrics["nsq_mpub", ("topic", topic)] += 1
    while retries > 0:
        # Get actual nsqd service's address and port
        si = services[s_index]
        if not nsqd_http_service_param.is_static(si):
            si = yield dcs.resolve(si, near=True)
        # Send message
        code, _, body = yield fetch(
            "http://%s/mpub?topic=%s&binary=true" % (si, topic),
            method="POST",
            body=msg,
            io_loop=io_loop,
            connect_timeout=config.nsqd.connect_timeout,
            request_timeout=config.nsqd.request_timeout,
        )
        if code == 200:
            break
        metrics["nsq_mpub_error", ("topic", topic)] += 1
        logger.error("Failed to pub to topic '%s': %s (Code=%d)", topic, body,
                     code)
        retries -= 1
        if retries > 0:
            yield tornado.gen.sleep(config.nsqd.pub_retry_delay)
            s_index = (s_index + 1) % num_services
    if code != 200:
        logger.error("Failed to pub to topic '%s'. Giving up", topic)
        metrics["nsq_mpub_fail", ("topic", topic)] += 1
        raise NSQPubError("NSQ Pub error: code=%s message=%s" % (code, body))
Пример #3
0
 def _start(self):
     self.ioloop = tornado.ioloop.IOLoop.instance()
     # Initialize DCS
     self.dcs = get_dcs(DEFAULT_DCS)
     # Activate service
     self.logger.warning("Activating stub service")
     self.logger.warning("Starting IOLoop")
     self.ioloop.add_callback(self.is_ready.set)
     self.ioloop.start()
Пример #4
0
 def _start(self):
     self.loop = asyncio.get_event_loop()
     # Initialize DCS
     self.dcs = get_dcs(DEFAULT_DCS)
     # Activate service
     self.logger.warning("Activating stub service")
     self.logger.warning("Starting IOLoop")
     self.loop.call_soon(self.is_ready.set)
     self.loop.run_forever()
Пример #5
0
 def start(self):
     """
     Run main server loop
     """
     self.startup_ts = perf_counter()
     parser = self.create_parser()
     self.add_arguments(parser)
     options = parser.parse_args(sys.argv[1:])
     cmd_options = vars(options)
     cmd_options.pop("args", ())
     # Bootstrap logging with --loglevel
     self.setup_logging(cmd_options["loglevel"])
     self.log_separator()
     # Setup timezone
     try:
         self.logger.info("Setting timezone to %s", config.timezone)
         setup_timezone()
     except ValueError as e:
         self.die(str(e))
     # Setup title
     self.set_proc_title()
     # Setup signal handlers
     self.setup_signal_handlers()
     self.on_start()
     # Starting IOLoop
     self.is_active = True
     if self.pooled:
         self.logger.warning("Running service %s (pool: %s)", self.name,
                             config.pool)
     else:
         self.logger.warning("Running service %s", self.name)
     try:
         setup_asyncio()
         self.loop = asyncio.get_event_loop()
         # Initialize DCS
         self.dcs = get_dcs(cmd_options["dcs"])
         # Activate service
         self.loop.create_task(self.activate())
         self.logger.warning("Starting IOLoop")
         self.loop.run_forever()
     except KeyboardInterrupt:
         self.logger.warning("Interrupted by Ctrl+C")
     except self.RegistrationError:
         self.logger.info("Registration failed")
     except Exception:
         error_report()
     finally:
         if self.loop:
             self.loop.create_task(self.deactivate())
     self.logger.warning("Service %s has been terminated", self.name)