Пример #1
0
    def _on_pika_message(self, channel, method, props, body):
        log.debug('PikaCient: Message received, delivery tag #%i : %r' % (method.delivery_tag, len(body)))

        correlation_id = getattr(props, 'correlation_id', None)
        if not self.callbacks_hash.has_key(correlation_id) and method.exchange != 'DLX':
            log.info('Got result for task "{0}", but no has callback'.format(correlation_id))
            return

        cb = self.callbacks_hash.pop(correlation_id)
        content_type = getattr(props, 'content_type', 'text/plain')

        if method.exchange == 'DLX':
            dl = props.headers['x-death'][0]
            body = ExpirationError("Dead letter received. Reason: {0}".format(dl.get('reason')))
            body.reason = dl.get('reason')
            body.time = dl.get('time')
            body.expiration = int(dl.get('original-expiration')) / 1000
        else:
            if props.content_encoding == 'gzip':
                body = zlib.decompress(body)

            if 'application/json' in content_type:
                body = json.loads(body)
            elif 'application/python-pickle' in content_type:
                body = pickle.loads(body)

        if isinstance(cb, Future):
            if isinstance(body, Exception):
                cb.set_exception(body)
            else:
                cb.set_result(body)
        else:
            out = cb(body, headers=props.headers)
            return out
Пример #2
0
    def _on_pika_message(self, channel, method, props, body):
        log.debug('PikaCient: Message received, delivery tag #%i : %r' %
                  (method.delivery_tag, len(body)))

        correlation_id = getattr(props, 'correlation_id', None)
        if correlation_id not in self.callbacks_hash and method.exchange != 'pubsub':
            if method.exchange != 'DLX':
                log.info(
                    'Got result for task "{0}", but no has callback'.format(
                        correlation_id))
            return

        if method.exchange == 'pubsub':
            cb = None
        else:
            cb = self.callbacks_hash.pop(correlation_id)

        content_type = getattr(props, 'content_type', 'text/plain')

        if method.exchange == 'DLX':
            dl = props.headers['x-death'][0]
            body = ExpirationError("Dead letter received. Reason: {0}".format(
                dl.get('reason')))
            body.reason = dl.get('reason')
            body.time = dl.get('time')
            body.expiration = int(dl.get('original-expiration')) / 1000
        else:
            if props.content_encoding == 'gzip':
                body = zlib.decompress(body)
            if 'application/json' in content_type:
                body = json.loads(body)
            elif 'application/python-pickle' in content_type:
                body = pickle.loads(body)

        channel.basic_ack(delivery_tag=method.delivery_tag)

        if method.exchange == 'pubsub':
            ch = props.headers.get('x-pubsub-channel-name', None)
            if ch in self.pubsub:
                for cb in self.pubsub[ch]:
                    try:
                        cb(body)
                    except Exception as e:
                        log.debug(traceback.format_exc())
                        log.error("Error in subscribed callback: {0}".format(
                            str(e)))
                return
        else:
            if isinstance(cb, Future):
                if isinstance(body, Exception):
                    cb.set_exception(body)
                else:
                    cb.set_result(body)
            else:
                out = cb(body, headers=props.headers)
                return out
Пример #3
0
    def _on_pika_message(self, channel, method, props, body):
        log.debug('PikaCient: Message received, delivery tag #%i : %r' % (
            method.delivery_tag, len(body)
        ))

        correlation_id = getattr(props, 'correlation_id', None)
        if correlation_id not in self.callbacks_hash and method.exchange != 'pubsub':
            if method.exchange != 'DLX':
                log.info('Got result for task "{0}", but no has callback'.format(correlation_id))
            return

        if method.exchange == 'pubsub':
            cb = None
        else:
            cb = self.callbacks_hash.pop(correlation_id)

        content_type = getattr(props, 'content_type', 'text/plain')

        if method.exchange == 'DLX':
            dl = props.headers['x-death'][0]
            body = ExpirationError(
                "Dead letter received. Reason: {0}".format(dl.get('reason'))
            )
            body.reason = dl.get('reason')
            body.time = dl.get('time')
            body.expiration = int(dl.get('original-expiration')) / 1000
        else:
            if props.content_encoding == 'gzip':
                body = zlib.decompress(body)
            if 'application/json' in content_type:
                body = json.loads(body)
            elif 'application/python-pickle' in content_type:
                body = pickle.loads(body)

        channel.basic_ack(delivery_tag=method.delivery_tag)

        if method.exchange == 'pubsub':
            ch = props.headers.get('x-pubsub-channel-name', None)
            if ch in self.pubsub:
                for cb in self.pubsub[ch]:
                    try:
                        cb(body)
                    except Exception as e:
                        log.debug(traceback.format_exc())
                        log.error("Error in subscribed callback: {0}".format(str(e)))
                return
        else:
            if isinstance(cb, Future):
                if isinstance(body, Exception):
                    cb.set_exception(body)
                else:
                    cb.set_result(body)
            else:
                out = cb(body, headers=props.headers)
                return out
Пример #4
0
    def _on_dlx_received(self, channel, method, props, body):
        correlation_id = getattr(props, 'correlation_id', None)

        if correlation_id in self.callbacks_hash:
            cb = self.callbacks_hash.pop(correlation_id)

            try:
                dl = props.headers['x-death'][0]
                body = ExpirationError(
                    "Dead letter received. Reason: {0}".format(
                        dl.get('reason')))
                body.reason = dl.get('reason')
                body.time = dl.get('time')
                body.expiration = int(dl.get('original-expiration')) / 1000

                cb.set_exception(body)
            finally:
                channel.basic_ack(delivery_tag=method.delivery_tag)

        else:
            log.error("Method callback %s is not found", correlation_id)
            channel.basic_ack(delivery_tag=method.delivery_tag)
            return
Пример #5
0
    def _on_dlx_received(self, channel, method, props, body):
        correlation_id = getattr(props, 'correlation_id', None)

        if correlation_id in self.callbacks_hash:
            cb = self.callbacks_hash.pop(correlation_id)

            try:
                dl = props.headers['x-death'][0]
                body = ExpirationError(
                    "Dead letter received. Reason: {0}".format(dl.get('reason'))
                )
                body.reason = dl.get('reason')
                body.time = dl.get('time')
                body.expiration = int(dl.get('original-expiration')) / 1000

                cb.set_exception(body)
            finally:
                channel.basic_ack(delivery_tag=method.delivery_tag)

        else:
            log.error("Method callback %s is not found", correlation_id)
            channel.basic_ack(delivery_tag=method.delivery_tag)
            return
Пример #6
0
    def _on_dlx_received(self, channel, method, props, body):
        correlation_id = getattr(props, "correlation_id", None)
        if correlation_id in self.callbacks_hash:
            cb = self.callbacks_hash.pop(correlation_id)
        else:
            log.error("Method callback %s is not found", correlation_id)
            channel.basic_ack(delivery_tag=method.delivery_tag)
            return

        try:
            dl = props.headers["x-death"][0]
            body = ExpirationError("Dead letter received. Reason: {0}".format(dl.get("reason")))
            body.reason = dl.get("reason")
            body.time = dl.get("time")
            body.expiration = int(dl.get("original-expiration")) / 1000

            if isinstance(cb, Future):
                tornado.ioloop.IOLoop.instance().add_callback(partial(cb.set_result, body))
            elif callable(cb):
                tornado.ioloop.IOLoop.instance().add_callback(partial(cb, body))
            else:
                log.error("Callback is not callable")
        finally:
            channel.basic_ack(delivery_tag=method.delivery_tag)