Project

General

Profile

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

Benjamin Dauvergne, 14 Apr 2020 10:17 AM

Download (2.71 KB)

View differences:

Subject: [PATCH 2/3] 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
36 36

  
37 37
import jsonfield
38 38

  
39
from passerelle.utils.defer import deferrable
39 40
import passerelle
40 41
import requests
41 42
from passerelle.compat import json_loads
......
727 728
    message = models.TextField(verbose_name='message')
728 729
    extra = jsonfield.JSONField(verbose_name='extras', default={})
729 730

  
731
    @classmethod
732
    @deferrable
733
    def safe_create(cls, **attr):
734
        cls.objects.create(**attr)
735

  
730 736
    class Meta:
731 737
        permissions = (
732 738
            ('view_resourcelog', 'Can view resource logs'),
......
843 849
                (exc_type, exc_value, tb) = sys.exc_info()
844 850
                attr['extra']['error_summary'] = traceback.format_exception_only(exc_type, exc_value)
845 851

  
846
            ResourceLog.objects.create(**attr)
852
            ResourceLog.safe_create(**attr)
847 853

  
848 854
        admins = settings.ADMINS
849 855
        logging_parameters = self.connector.logging_parameters
tests/test_proxylogger.py
256 256
    base_logger.debug('test')
257 257
    log = ResourceLog.objects.latest('id')
258 258
    assert log.extra == {}
259

  
260

  
261
def test_log_in_transaction(transactional_db, connector):
262
    from passerelle.utils.defer import deferrable_barrier
263
    from django.db import transaction
264

  
265
    qs = ResourceLog.objects.filter(message='coucou')
266
    # without deferrable_barrier logs are lost
267
    assert not qs.exists()
268
    try:
269
        with transaction.atomic():
270
            connector.logger.info('coucou')
271
            raise Exception
272
            assert qs.exists()
273
    except Exception:
274
        pass
275
    assert not qs.exists()
276

  
277
    # with deferrable_barrier logs are kept
278
    try:
279
        with deferrable_barrier:
280
            with transaction.atomic():
281
                connector.logger.info('coucou')
282
                raise Exception
283
                assert qs.exists()
284
    except Exception:
285
        pass
286
    assert qs.exists()
259
-