Ejemplo n.º 1
0
def test_consumer(mocker):
    factory_mock = mocker.patch(
        'dj_cqrs.controller.consumer.route_signal_to_replica_model')
    consume(TransportPayload('a', 'b', {}, 'c', previous_data={'e': 'f'}))

    factory_mock.assert_called_once_with('a',
                                         'b', {},
                                         previous_data={'e': 'f'})
Ejemplo n.º 2
0
    def _consume_message(cls, body, message):
        try:
            dct = ujson.loads(body)
        except ValueError:
            logger.error("CQRS couldn't be parsed: {0}.".format(body))
            message.reject()
            return

        required_keys = {'instance_pk', 'signal_type', 'cqrs_id', 'instance_data'}
        for key in required_keys:
            if key not in dct:
                msg = "CQRS couldn't proceed, %s isn't found in body: %s."
                logger.error(msg, key, body)
                message.reject()
                return

        payload = TransportPayload(
            dct['signal_type'],
            dct['cqrs_id'],
            dct['instance_data'],
            dct.get('instance_pk'),
            previous_data=dct.get('previous_data'),
            correlation_id=dct.get('correlation_id'),
        )

        cls.log_consumed(payload)
        instance = consumer.consume(payload)

        if instance:
            message.ack()
            cls.log_consumed_accepted(payload)
        else:
            message.reject()
            cls.log_consumed_denied(payload)
Ejemplo n.º 3
0
    def _consume_message(cls, ch, method, properties, body):
        try:
            dct = ujson.loads(body)
            for key in ('signal_type', 'cqrs_id', 'instance_data'):
                if key not in dct:
                    raise ValueError

            if 'instance_pk' not in dct:
                logger.warning('CQRS deprecated package structure.')

        except ValueError:
            logger.error("CQRS couldn't be parsed: {}.".format(body))
            ch.basic_reject(delivery_tag=method.delivery_tag, requeue=False)
            return

        payload = TransportPayload(
            dct['signal_type'], dct['cqrs_id'], dct['instance_data'], dct.get('instance_pk'),
            previous_data=dct.get('previous_data'),
        )

        cls.log_consumed(payload)

        instance = None
        try:
            instance = consumer.consume(payload)
        except Exception:
            logger.error('CQRS service exception', exc_info=True)

        if instance:
            ch.basic_ack(delivery_tag=method.delivery_tag)
            cls.log_consumed_accepted(payload)
        else:
            ch.basic_nack(delivery_tag=method.delivery_tag)
            cls.log_consumed_denied(payload)
Ejemplo n.º 4
0
    def _consume_message(cls, body, message):
        try:
            dct = ujson.loads(body)
            for key in ('signal_type', 'cqrs_id', 'instance_data'):
                if key not in dct:
                    raise ValueError

            if 'instance_pk' not in dct:
                logger.warning('CQRS deprecated package structure.')

        except ValueError:
            logger.error("CQRS couldn't be parsed: {}.".format(body))
            message.reject()
            return

        payload = TransportPayload(
            dct['signal_type'], dct['cqrs_id'], dct['instance_data'], dct.get('instance_pk'),
            previous_data=dct.get('previous_data'),
        )

        cls.log_consumed(payload)
        instance = consumer.consume(payload)

        if instance:
            message.ack()
            cls.log_consumed_accepted(payload)
        else:
            message.reject()
            cls.log_consumed_denied(payload)
Ejemplo n.º 5
0
def test_changed_payload_data_during_consume(mocker):
    def change_data(*args, **kwargs):
        instance_data = args[2]
        instance_data['instance_key'] = 'changed instance'
        kwargs['previous_data']['previous_key'] = 'changed previous'

    factory_mock = mocker.patch(
        'dj_cqrs.controller.consumer.route_signal_to_replica_model',
        side_effect=change_data,
    )

    payload = TransportPayload(
        SignalType.SAVE,
        cqrs_id='b',
        instance_data={'instance_key': 'initial instance'},
        instance_pk='c',
        previous_data={'previous_key': 'initial previous'},
    )
    consume(payload)

    assert factory_mock.call_count == 1
    assert payload.instance_data == {'instance_key': 'initial instance'}
    assert payload.previous_data == {'previous_key': 'initial previous'}
Ejemplo n.º 6
0
    def _consume_message(cls, ch, method, properties, body, delay_queue):
        try:
            dct = ujson.loads(body)
        except ValueError:
            logger.error("CQRS couldn't be parsed: {0}.".format(body))
            ch.basic_reject(delivery_tag=method.delivery_tag, requeue=False)
            return

        required_keys = {
            'instance_pk', 'signal_type', 'cqrs_id', 'instance_data'
        }
        for key in required_keys:
            if key not in dct:
                msg = "CQRS couldn't proceed, %s isn't found in body: %s."
                logger.error(msg, key, body)
                ch.basic_reject(delivery_tag=method.delivery_tag,
                                requeue=False)
                return

        payload = TransportPayload.from_message(dct)
        cls.log_consumed(payload)

        delivery_tag = method.delivery_tag
        if payload.is_expired():
            cls._add_to_dead_letter_queue(ch, payload)
            cls._nack(ch, delivery_tag)
            return

        instance, exception = None, None
        try:
            instance = consumer.consume(payload)
        except Exception as e:
            exception = e
            logger.error("CQRS service exception", exc_info=True)

        if instance and exception is None:
            cls._ack(ch, delivery_tag, payload)
        else:
            cls._fail_message(
                ch,
                delivery_tag,
                payload,
                exception,
                delay_queue,
            )
Ejemplo n.º 7
0
 def consume(payload):
     consumer.consume(payload)