Project

General

Profile

0003-base-use-deferrable-to-create-ResourceLog-objects-31.patch

Benjamin Dauvergne, 14 May 2019 10:13 AM

Download (2.79 KB)

View differences:

Subject: [PATCH 3/4] base: use deferrable to create ResourceLog objects
 (#31204)

 passerelle/base/models.py |  8 +++++++-
 tests/test_proxylogger.py | 28 ++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
passerelle/base/models.py
31 31

  
32 32
import jsonfield
33 33

  
34
from passerelle.utils.defer import deferrable
34 35
import passerelle
35 36
import requests
36 37

  
......
592 593
    message = models.TextField(max_length=2048, verbose_name='message')
593 594
    extra = jsonfield.JSONField(verbose_name='extras', default={})
594 595

  
596
    @classmethod
597
    @deferrable
598
    def safe_create(cls, **attr):
599
        cls.objects.create(**attr)
600

  
595 601
    class Meta:
596 602
        permissions = (
597 603
            ('view_resourcelog', 'Can view resource logs'),
......
686 692
                (exc_type, exc_value, tb) = sys.exc_info()
687 693
                attr['extra']['error_summary'] = traceback.format_exception_only(exc_type, exc_value)
688 694

  
689
            ResourceLog.objects.create(**attr)
695
            ResourceLog.safe_create(**attr)
690 696

  
691 697
        admins = settings.ADMINS
692 698
        logging_parameters = self.connector.logging_parameters
tests/test_proxylogger.py
83 83
    assert len(ResourceLog.objects.all()) == 3
84 84
    assert ResourceLog.objects.all()[2].level == 'info'
85 85
    assert ResourceLog.objects.all()[2].message == u'connector "éléphant" (Feed) is back up'
86

  
87

  
88
def test_log_in_transaction(transactional_db, connector):
89
    from passerelle.utils.defer import deferrable_barrier
90
    from django.db import transaction
91

  
92
    qs = ResourceLog.objects.filter(message='coucou')
93
    # without deferrable_barrier logs are lost
94
    assert not qs.exists()
95
    try:
96
        with transaction.atomic():
97
            connector.logger.info('coucou')
98
            raise Exception
99
            assert qs.exists()
100
    except Exception:
101
        pass
102
    assert not qs.exists()
103

  
104
    # with deferrable_barrier logs are kept
105
    try:
106
        with deferrable_barrier:
107
            with transaction.atomic():
108
                connector.logger.info('coucou')
109
                raise Exception
110
                assert qs.exists()
111
    except Exception:
112
        pass
113
    assert qs.exists()
86
-