Project

General

Profile

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

Benjamin Dauvergne, 04 Oct 2019 02:04 AM

Download (2.81 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
32 32

  
33 33
import jsonfield
34 34

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

  
......
694 695
    message = models.TextField(max_length=2048, verbose_name='message')
695 696
    extra = jsonfield.JSONField(verbose_name='extras', default={})
696 697

  
698
    @classmethod
699
    @deferrable
700
    def safe_create(cls, **attr):
701
        cls.objects.create(**attr)
702

  
697 703
    class Meta:
698 704
        permissions = (
699 705
            ('view_resourcelog', 'Can view resource logs'),
......
790 796
                (exc_type, exc_value, tb) = sys.exc_info()
791 797
                attr['extra']['error_summary'] = traceback.format_exception_only(exc_type, exc_value)
792 798

  
793
            ResourceLog.objects.create(**attr)
799
            ResourceLog.safe_create(**attr)
794 800

  
795 801
        admins = settings.ADMINS
796 802
        logging_parameters = self.connector.logging_parameters
tests/test_proxylogger.py
188 188
    assert last_count4 == last_count3 + 1
189 189
    assert ResourceLog.objects.latest('id').level == 'info'
190 190
    assert ResourceLog.objects.latest('id').message == 'GET http://example.net/ (=> 200)'
191

  
192

  
193
def test_log_in_transaction(transactional_db, connector):
194
    from passerelle.utils.defer import deferrable_barrier
195
    from django.db import transaction
196

  
197
    qs = ResourceLog.objects.filter(message='coucou')
198
    # without deferrable_barrier logs are lost
199
    assert not qs.exists()
200
    try:
201
        with transaction.atomic():
202
            connector.logger.info('coucou')
203
            raise Exception
204
            assert qs.exists()
205
    except Exception:
206
        pass
207
    assert not qs.exists()
208

  
209
    # with deferrable_barrier logs are kept
210
    try:
211
        with deferrable_barrier:
212
            with transaction.atomic():
213
                connector.logger.info('coucou')
214
                raise Exception
215
                assert qs.exists()
216
    except Exception:
217
        pass
218
    assert qs.exists()
191
-