Ejemplo n.º 1
0
 def test_close(self, _find):
     _impl = Mock()
     plugin = Mock()
     plugin.Reader.return_value = _impl
     _find.return_value = plugin
     url = TEST_URL
     node = Node('')
     reader = Reader(node, url)
     # soft
     reader.close()
     _impl.close.assert_called_with()
Ejemplo n.º 2
0
 def test_close(self, _find):
     _impl = Mock()
     plugin = Mock()
     plugin.Reader.return_value = _impl
     _find.return_value = plugin
     url = TEST_URL
     node = Node('')
     reader = Reader(node, url)
     # soft
     reader.close()
     _impl.close.assert_called_with()
Ejemplo n.º 3
0
class ConsumerThread(Thread):
    """
    An AMQP (abstract) consumer.
    """

    def __init__(self, node, url, wait=3):
        """
        :param node: An AMQP queue.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker URL.
        :type url: str
        :param wait: Number of seconds to wait for a message.
        :type wait: int
        """
        Thread.__init__(self, name=node.name)
        self.url = url
        self.node = node
        self.wait = wait
        self.authenticator = None
        self.reader = None
        self.setDaemon(True)

    def shutdown(self):
        """
        Shutdown the consumer.
        """
        self.abort()

    @released
    def run(self):
        """
        Main consumer loop.
        """
        self.reader = Reader(self.node, self.url)
        self.reader.authenticator = self.authenticator
        self.open()
        try:
            while not Thread.aborted():
                self.read()
        finally:
            self.close()

    def open(self):
        """
        Open the reader.
        """
        while not Thread.aborted():
            try:
                self.reader.open()
                break
            except Exception:
                log.exception(self.getName())
                sleep(30)

    def close(self):
        """
        Close the reader.
        """
        try:
            self.reader.close()
        except Exception:
            log.exception(self.getName())

    def read(self):
        """
        Read and process incoming documents.
        """
        try:
            wait = self.wait
            reader = self.reader
            message, document = reader.next(wait)
            if message is None:
                # wait expired
                return
            log.debug("{%s} read: %s", self.getName(), document)
            self.dispatch(document)
            message.ack()
        except DocumentError, de:
            self.rejected(de.code, de.description, de.document, de.details)
        except Exception:
            log.exception(self.getName())
            sleep(60)
            self.close()
            self.open()
Ejemplo n.º 4
0
class ConsumerThread(Thread):
    """
    An AMQP (abstract) consumer.
    """

    def __init__(self, node, url):
        """
        :param node: An AMQP queue.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker URL.
        :type url: str
        """
        Thread.__init__(self, name=node.name)
        self.url = url
        self.node = node
        self.authenticator = None
        self._reader = None
        self.setDaemon(True)

    def shutdown(self):
        """
        Shutdown the consumer.
        """
        self.abort()

    @released
    def run(self):
        """
        Main consumer loop.
        """
        self._reader = Reader(self.node, self.url)
        self._reader.authenticator = self.authenticator
        self._open()
        try:
            while not Thread.aborted():
                self._read()
        finally:
            self._close()

    def _open(self):
        """
        Open the reader.
        """
        while not Thread.aborted():
            try:
                self._reader.open()
                break
            except Exception:
                log.exception(self.getName())
                sleep(60)

    def _close(self):
        """
        Close the reader.
        """
        try:
            self._reader.close()
        except Exception:
            log.exception(self.getName())

    def _read(self):
        """
        Read and process incoming documents.
        """
        try:
            message, document = self._reader.next(10)
            if message is None:
                return
            log.debug('{%s} read: %s', self.getName(), document)
            self.dispatch(document)
            message.ack()
        except InvalidDocument, invalid:
            self._rejected(invalid.code, invalid.description, invalid.document, invalid.details)
        except Exception:
            log.exception(self.getName())
            sleep(60)
            self._close()
            self._open()
Ejemplo n.º 5
0
class ConsumerThread(Thread):
    """
    An AMQP (abstract) consumer.
    """

    def __init__(self, node, url, wait=3):
        """
        :param node: An AMQP queue.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker URL.
        :type url: str
        :param wait: Number of seconds to wait for a message.
        :type wait: int
        """
        Thread.__init__(self, name=node.name)
        self.url = url
        self.node = node
        self.wait = wait
        self.authenticator = None
        self.reader = None
        self.setDaemon(True)

    def shutdown(self):
        """
        Shutdown the consumer.
        """
        self.abort()

    @released
    def run(self):
        """
        Main consumer loop.
        """
        self.reader = Reader(self.node, self.url)
        self.reader.authenticator = self.authenticator
        self.open()
        try:
            while not Thread.aborted():
                self.read()
        finally:
            self.close()

    def open(self):
        """
        Open the reader.
        """
        while not Thread.aborted():
            try:
                self.reader.open()
                break
            except Exception:
                log.exception(self.getName())
                sleep(30)

    def close(self):
        """
        Close the reader.
        """
        try:
            self.reader.close()
        except Exception:
            log.exception(self.getName())

    def read(self):
        """
        Read and process incoming documents.
        """
        try:
            wait = self.wait
            reader = self.reader
            message, document = reader.next(wait)
            if message is None:
                # wait expired
                return
            log.debug('{%s} read: %s', self.getName(), document)
            self.dispatch(document)
            message.ack()
        except DocumentError, de:
            self.rejected(de.code, de.description, de.document, de.details)
        except Exception:
            log.exception(self.getName())
            sleep(60)
            self.close()
            self.open()
Ejemplo n.º 6
0
class ConsumerThread(Thread):
    """
    An AMQP (abstract) consumer.
    """
    def __init__(self, node, url):
        """
        :param node: An AMQP queue.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker URL.
        :type url: str
        """
        Thread.__init__(self, name=node.name)
        self.url = url
        self.node = node
        self.authenticator = None
        self._reader = None
        self.setDaemon(True)

    def shutdown(self):
        """
        Shutdown the consumer.
        """
        self.abort()

    @released
    def run(self):
        """
        Main consumer loop.
        """
        self._reader = Reader(self.node, self.url)
        self._reader.authenticator = self.authenticator
        self._open()
        try:
            while not Thread.aborted():
                self._read()
        finally:
            self._close()

    def _open(self):
        """
        Open the reader.
        """
        while not Thread.aborted():
            try:
                self._reader.open()
                break
            except Exception:
                log.exception(self.getName())
                sleep(60)

    def _close(self):
        """
        Close the reader.
        """
        try:
            self._reader.close()
        except Exception:
            log.exception(self.getName())

    def _read(self):
        """
        Read and process incoming documents.
        """
        try:
            message, document = self._reader.next(10)
            if message is None:
                return
            log.debug('{%s} read: %s', self.getName(), document)
            self.dispatch(document)
            message.ack()
        except InvalidDocument, invalid:
            self._rejected(invalid.code, invalid.description, invalid.document,
                           invalid.details)
        except Exception:
            log.exception(self.getName())
            sleep(60)
            self._close()
            self._open()