Exemplo n.º 1
0
Arquivo: queue.py Projeto: blt/beaver
def run_queue(queue, beaver_config, file_config, logger=None):
    transport = None
    try:
        logger.debug("Logging using the {0} transport".format(beaver_config.get("transport")))
        transport = create_transport(beaver_config, file_config, logger=logger)

        failure_count = 0
        while True:
            command, data = queue.get()
            if command == "callback":
                try:
                    transport.callback(*data)
                except TransportException:
                    failure_count = failure_count + 1
                    if failure_count > int(beaver_config.get("max_failure")):
                        failure_count = int(beaver_config.get("max_failure"))

                    sleep_time = int(beaver_config.get("respawn_delay")) ** failure_count
                    logger.info("Caught transport exception, respawning in %d seconds" % sleep_time)

                    try:
                        time.sleep(sleep_time)
                    except KeyboardInterrupt:
                        logger.info("User cancelled respawn.")
                        transport.interrupt()

                        sys.exit(0)
    except KeyboardInterrupt:
        logger.debug("Queue Interruped")
        if transport is not None:
            transport.interrupt()

        logger.debug("Queue Shutdown")
Exemplo n.º 2
0
    def __init__(self,
                 host='localhost',
                 userid='guest',
                 password='******',
                 login_method='AMQPLAIN',
                 login_response=None,
                 virtual_host='/',
                 locale='en_US',
                 client_properties=None,
                 ssl=False,
                 insist=False,
                 connect_timeout=None,
                 **kwargs):
        """
        Create a connection to the specified host, which should be
        a 'host[:port]', such as 'localhost', or '1.2.3.4:5672'
        (defaults to 'localhost', if a port is not specified then
        5672 is used)

        If login_response is not specified, one is built up for you from
        userid and password if they are present.

        The 'ssl' parameter may be simply True/False, or for Python >= 2.6
        a dictionary of options to pass to ssl.wrap_socket() such as
        requiring certain certificates.

        """
        if (login_response is None) \
        and (userid is not None) \
        and (password is not None):
            login_response = AMQPWriter()
            login_response.write_table({'LOGIN': userid, 'PASSWORD': password})
            login_response = login_response.getvalue()[4:]  #Skip the length
            #at the beginning

        d = {}
        d.update(LIBRARY_PROPERTIES)
        if client_properties:
            d.update(client_properties)

        self.known_hosts = ''

        while True:
            self.channels = {}
            # The connection object itself is treated as channel 0
            super(Connection, self).__init__(self, 0)

            self.transport = None

            # Properties set in the Tune method
            self.channel_max = 65535
            self.frame_max = 131072
            self.heartbeat = 0

            # Properties set in the Start method
            self.version_major = 0
            self.version_minor = 0
            self.server_properties = {}
            self.mechanisms = []
            self.locales = []

            # Let the transport.py module setup the actual
            # socket connection to the broker.
            #
            self.transport = create_transport(host, connect_timeout, ssl)

            self.method_reader = MethodReader(self.transport)
            self.method_writer = MethodWriter(self.transport, self.frame_max)

            self.wait(allowed_methods=[
                (10, 10),  # start
            ])

            self._x_start_ok(d, login_method, login_response, locale)

            self._wait_tune_ok = True
            while self._wait_tune_ok:
                self.wait(allowed_methods=[
                    (10, 20),  # secure
                    (10, 30),  # tune
                ])

            host = self._x_open(virtual_host, insist=insist)
            if host is None:
                # we weren't redirected
                return

            # we were redirected, close the socket, loop and try again
            try:
                self.close()
            except Exception:
                pass
Exemplo n.º 3
0
    def __init__(
        self,
        host="localhost",
        userid="guest",
        password="******",
        login_method="AMQPLAIN",
        login_response=None,
        virtual_host="/",
        locale="en_US",
        client_properties=None,
        ssl=False,
        insist=False,
        connect_timeout=None,
        **kwargs
    ):
        """
        Create a connection to the specified host, which should be
        a 'host[:port]', such as 'localhost', or '1.2.3.4:5672'
        (defaults to 'localhost', if a port is not specified then
        5672 is used)

        If login_response is not specified, one is built up for you from
        userid and password if they are present.

        """
        if (login_response is None) and (userid is not None) and (password is not None):
            login_response = AMQPWriter()
            login_response.write_table({"LOGIN": userid, "PASSWORD": password})
            login_response = login_response.getvalue()[4:]  # Skip the length
            # at the beginning

        d = {}
        d.update(LIBRARY_PROPERTIES)
        if client_properties:
            d.update(client_properties)

        self.known_hosts = ""

        while True:
            self.channels = {}
            # The connection object itself is treated as channel 0
            super(Connection, self).__init__(self, 0)

            self.transport = None

            # Properties set in the Tune method
            self.channel_max = 65535
            self.frame_max = 131072
            self.heartbeat = 0

            # Properties set in the Start method
            self.version_major = 0
            self.version_minor = 0
            self.server_properties = {}
            self.mechanisms = []
            self.locales = []

            # Let the transport.py module setup the actual
            # socket connection to the broker.
            #
            self.transport = create_transport(host, connect_timeout, ssl)

            self.method_reader = MethodReader(self.transport)
            self.method_writer = MethodWriter(self.transport, self.frame_max)

            self.wait(allowed_methods=[(10, 10)])  # start

            self._x_start_ok(d, login_method, login_response, locale)

            self._wait_tune_ok = True
            while self._wait_tune_ok:
                self.wait(allowed_methods=[(10, 20), (10, 30)])  # secure  # tune

            host = self._x_open(virtual_host, insist=insist)
            if host is None:
                # we weren't redirected
                return

            # we were redirected, close the socket, loop and try again
            try:
                self.close()
            except Exception:
                pass
Exemplo n.º 4
0
def run_queue(queue, beaver_config, file_config, logger=None):
    signal.signal(signal.SIGTERM, signal.SIG_DFL)
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    signal.signal(signal.SIGQUIT, signal.SIG_DFL)

    last_update_time = int(time.time())
    queue_timeout = beaver_config.get('queue_timeout')
    wait_timeout = beaver_config.get('wait_timeout')

    transport = None
    try:
        logger.debug("Logging using the {0} transport".format(beaver_config.get('transport')))
        transport = create_transport(beaver_config, file_config, logger=logger)

        failure_count = 0
        while True:
            if not transport.valid():
                logger.info("Transport connection issues, stopping queue")
                break

            if int(time.time()) - last_update_time > queue_timeout:
                logger.info("Queue timeout of '{0}' seconds exceeded, stopping queue".format(queue_timeout))
                break

            try:
                command, data = queue.get(block=True, timeout=wait_timeout)
                last_update_time = int(time.time())
                logger.debug("Last update time now {0}".format(last_update_time))
            except Queue.Empty:
                logger.debug("No data")
                continue

            if command == "callback":
                try:
                    transport.callback(*data)
                except TransportException:
                    failure_count = failure_count + 1
                    if failure_count > beaver_config.get('max_failure'):
                        failure_count = beaver_config.get('max_failure')

                    sleep_time = int(beaver_config.get('respawn_delay')) ** failure_count
                    logger.info("Caught transport exception, respawning in %d seconds" % sleep_time)

                    try:
                        time.sleep(sleep_time)
                        transport.reconnect()
                    except KeyboardInterrupt:
                        logger.info("User cancelled respawn.")
                        transport.interrupt()

                        sys.exit(0)
            elif command == "addglob":
                transport.addglob(*data)
            elif command == "exit":
                break
    except KeyboardInterrupt:
        logger.debug("Queue Interruped")
        if transport is not None:
            transport.interrupt()

        logger.debug("Queue Shutdown")