Example #1
0
def context_serve(options, context):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface 
    and starts a server within that context.
    
    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough) 
    context object for the non-daemon use case. 
    
    @param options: The compiled collection of options that need to be parsed. 
    @type options: C{ConfigParser}
    
    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}
    
    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if options.debug else logging.INFO
            init_logging(logfile=options.logfile,
                         loglevel=level,
                         configfile=options.configfile)

            server = server_from_config()
            logger().info("Stomp server listening on %s:%s" %
                          server.server_address)
            server.serve_forever()
    except (KeyboardInterrupt, SystemExit):
        logger().info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception, e:
        logger().error("Stomp server stopped due to error: %s" % e)
        logger().exception(e)
        raise SystemExit()
Example #2
0
def context_serve(options, context):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface 
    and starts a server within that context.
    
    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough) 
    context object for the non-daemon use case. 
    
    @param options: The compiled collection of options that need to be parsed. 
    @type options: C{ConfigParser}
    
    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}
    
    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if options.debug else logging.INFO
            init_logging(logfile=options.logfile, loglevel=level, configfile=options.configfile)
            
            server = server_from_config()
            logger().info("Stomp server listening on %s:%s" % server.server_address)
            server.serve_forever()
    except (KeyboardInterrupt, SystemExit):
        logger().info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception, e:
        logger().error("Stomp server stopped due to error: %s" % e)
        logger().exception(e)
        raise SystemExit()
Example #3
0
File: start.py Project: hozn/coilmq
def context_serve(context, configfile, listen_addr, listen_port, logfile,
                  debug, daemon, uid, gid, pidfile, umask, rundir):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface 
    and starts a server within that context.

    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough) 
    context object for the non-daemon use case. 

    @param options: The compiled collection of options that need to be parsed. 
    @type options: C{ConfigParser}

    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}

    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    global global_config

    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if debug else logging.INFO
            init_logging(logfile=logfile, loglevel=level,
                         configfile=configfile)

            server = server_from_config()
            logger.info("Stomp server listening on %s:%s" % server.server_address)

            if debug:
                poll_interval = float(global_config.get(
                        'coilmq', 'debug.stats_poll_interval'))
                if poll_interval:  # Setting poll_interval to 0 effectively disables it.
                    def diagnostic_loop(server):
                        log = logger
                        while True:
                            log.debug(
                                    "Stats heartbeat -------------------------------")
                            store = server.queue_manager.store
                            for dest in store.destinations():
                                log.debug("Queue %s: size=%s, subscribers=%s" % (
                                    dest, store.size(dest), server.queue_manager.subscriber_count(dest)))

                            # TODO: Add number of subscribers?

                            time.sleep(poll_interval)

                    diagnostic_thread = threading.Thread(
                            target=diagnostic_loop, name='DiagnosticThread', args=(server,))
                    diagnostic_thread.daemon = True
                    diagnostic_thread.start()

            server.serve_forever()

    except (KeyboardInterrupt, SystemExit):
        logger.info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception as e:
        logger.error("Stomp server stopped due to error: %s" % e)
        logger.exception(e)
        raise SystemExit()
    finally:
        if server:
            server.server_close()
Example #4
0
def context_serve(options, context):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface 
    and starts a server within that context.
    
    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough) 
    context object for the non-daemon use case. 
    
    @param options: The compiled collection of options that need to be parsed. 
    @type options: C{ConfigParser}
    
    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}
    
    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    global global_config

    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if options.debug else logging.INFO
            init_logging(logfile=options.logfile,
                         loglevel=level,
                         configfile=options.configfile)

            server = server_from_config()
            logger().info("Stomp server listening on %s:%s" %
                          server.server_address)

            if options.debug:
                poll_interval = float(
                    global_config.get('coilmq', 'debug.stats_poll_interval'))
                if poll_interval:  # Setting poll_interval to 0 effectively disables it.

                    def diagnostic_loop(server):
                        log = logger()
                        while True:
                            log.debug(
                                "Stats heartbeat -------------------------------"
                            )
                            store = server.queue_manager.store
                            for dest in store.destinations():
                                log.debug(
                                    "Queue %s: size=%s, subscribers=%s" %
                                    (dest, store.size(dest),
                                     server.queue_manager.subscriber_count(
                                         dest)))

                            # TODO: Add number of subscribers?

                            time.sleep(poll_interval)

                    diagnostic_thread = threading.Thread(
                        target=diagnostic_loop,
                        name='DiagnosticThread',
                        args=(server, ))
                    diagnostic_thread.daemon = True
                    diagnostic_thread.start()

            server.serve_forever()

    except (KeyboardInterrupt, SystemExit):
        logger().info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception, e:
        logger().error("Stomp server stopped due to error: %s" % e)
        logger().exception(e)
        raise SystemExit()