예제 #1
0
    def send_response(self, result, exc_info):

        error = None
        if exc_info is not None:
            error = serialize(exc_info[1])

        # disaster avoidance serialization check: `result` must be
        # serializable, otherwise the container will commit suicide assuming
        # unrecoverable errors (and the message will be requeued for another
        # victim)

        try:
            kombu.serialization.dumps(result, self.serializer)
        except Exception:
            exc_info = sys.exc_info()
            # `error` below is guaranteed to serialize to json
            error = serialize(UnserializableValueError(result))
            result = None

        payload = {'result': result, 'error': error}

        routing_key = self.message.properties['reply_to']
        correlation_id = self.message.properties.get('correlation_id')

        publisher = self.publisher_cls(self.amqp_uri,
                                       ssl_params=self.ssl_params)

        publisher.publish(payload,
                          serializer=self.serializer,
                          exchange=self.exchange,
                          routing_key=routing_key,
                          correlation_id=correlation_id)

        return result, exc_info
예제 #2
0
def test_unserializable_value_error():

    # normal value
    value = "value"
    exc = UnserializableValueError(value)

    assert exc.repr_value == "'value'"
    assert str(exc) == "Unserializable value: `'value'`"

    # un-repr-able value
    class CannotRepr(object):
        def __repr__(self):
            raise Exception('boom')

    bad_value = CannotRepr()
    exc = UnserializableValueError(bad_value)

    assert exc.repr_value == "[__repr__ failed]"
    assert str(exc) == "Unserializable value: `[__repr__ failed]`"
예제 #3
0
    def send_response(self, result, exc_info, **kwargs):

        error = None
        if exc_info is not None:
            error = serialize(exc_info[1])

        # disaster avoidance serialization check: `result` must be
        # serializable, otherwise the container will commit suicide assuming
        # unrecoverable errors (and the message will be requeued for another
        # victim)

        serializer = self.config.get(SERIALIZER_CONFIG_KEY, DEFAULT_SERIALIZER)

        try:
            kombu.serialization.dumps(result, serializer)
        except Exception:
            exc_info = sys.exc_info()
            # `error` below is guaranteed to serialize to json
            error = serialize(UnserializableValueError(result))
            result = None

        conn = Connection(self.config[AMQP_URI_CONFIG_KEY])

        exchange = get_rpc_exchange(self.config)

        retry = kwargs.pop('retry', True)
        retry_policy = kwargs.pop('retry_policy', DEFAULT_RETRY_POLICY)

        with producers[conn].acquire(block=True) as producer:

            routing_key = self.message.properties['reply_to']
            correlation_id = self.message.properties.get('correlation_id')

            msg = {'result': result, 'error': error}

            _log.debug('publish response %s:%s', routing_key, correlation_id)
            producer.publish(msg,
                             retry=retry,
                             retry_policy=retry_policy,
                             exchange=exchange,
                             routing_key=routing_key,
                             serializer=serializer,
                             correlation_id=correlation_id,
                             **kwargs)

        return result, exc_info
예제 #4
0
    def send_response(self, result, exc_info, **kwargs):

        error = None
        if exc_info is not None:
            error = serialize(exc_info[1])

        # disaster avoidance serialization check: `result` must be
        # serializable, otherwise the container will commit suicide assuming
        # unrecoverable errors (and the message will be requeued for another
        # victim)

        try:
            kombu.serialization.dumps(result, self.serializer)
        except Exception:
            exc_info = sys.exc_info()
            # `error` below is guaranteed to serialize to json
            error = serialize(UnserializableValueError(result))
            result = None

        exchange = get_rpc_exchange(self.config)

        retry = kwargs.pop('retry', self.retry)
        retry_policy = kwargs.pop('retry_policy', self.retry_policy)

        with get_producer(self.amqp_uri, self.use_confirms) as producer:

            routing_key = self.message.properties['reply_to']
            correlation_id = self.message.properties.get('correlation_id')

            msg = {'result': result, 'error': error}

            _log.debug('publish response %s:%s', routing_key, correlation_id)
            producer.publish(msg,
                             retry=retry,
                             retry_policy=retry_policy,
                             exchange=exchange,
                             routing_key=routing_key,
                             serializer=self.serializer,
                             correlation_id=correlation_id,
                             **kwargs)

        return result, exc_info
예제 #5
0
    def send_response(self, result, exc_info, **kwargs):
        error = None
        if exc_info is not None:
            error = serialize(exc_info[1])
            logger.warning("Sending error response %s", error)

        serializer = self.config.get(SERIALIZER_CONFIG_KEY, DEFAULT_SERIALIZER)

        try:
            kombu.serialization.dumps(result, serializer)
        except Exception:
            exc_info = sys.exc_info()
            error = serialize(UnserializableValueError(result))
            result = None

        conn = Connection(self.config[AMQP_URI_CONFIG_KEY])

        exchange = DEFAULT_EXCHANGE

        retry = kwargs.pop('retry', True)
        retry_policy = kwargs.pop('retry_policy', DEFAULT_RETRY_POLICY)

        with producers[conn].acquire(block=True) as producer:
            routing_key = self.message.properties['reply_to']
            correlation_id = self.message.properties.get('correlation_id')

            msg = {'result': result, 'error': error}

            logger.info("Sending message to %s %s: %s", exchange, routing_key,
                        msg)

            producer.publish(msg,
                             retry=retry,
                             retry_policy=retry_policy,
                             exchange=exchange,
                             routing_key=routing_key,
                             serializer=serializer,
                             correlation_id=correlation_id,
                             **kwargs)

        return result, exc_info