mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 04:22:01 -06:00
Simplify changelog cleanup logic
This commit is contained in:
parent
22b8a45a71
commit
3078e366e2
@ -45,7 +45,7 @@ def purge_objectchange_cache(sender, **kwargs):
|
|||||||
"""
|
"""
|
||||||
Delete any queued object changes waiting to be written.
|
Delete any queued object changes waiting to be written.
|
||||||
"""
|
"""
|
||||||
_thread_locals.changed_objects = None
|
_thread_locals.changed_objects = []
|
||||||
|
|
||||||
|
|
||||||
class ObjectChangeMiddleware(object):
|
class ObjectChangeMiddleware(object):
|
||||||
@ -84,9 +84,8 @@ class ObjectChangeMiddleware(object):
|
|||||||
# Process the request
|
# Process the request
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
|
|
||||||
# If the change cache has been purged (e.g. due to an exception) abort the logging of all changes resulting from
|
# If the change cache is empty, there's nothing more we need to do.
|
||||||
# this request.
|
if not _thread_locals.changed_objects:
|
||||||
if _thread_locals.changed_objects is None:
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# Create records for any cached objects that were created/updated.
|
# Create records for any cached objects that were created/updated.
|
||||||
@ -108,8 +107,9 @@ class ObjectChangeMiddleware(object):
|
|||||||
elif objectchange.action == OBJECTCHANGE_ACTION_DELETE:
|
elif objectchange.action == OBJECTCHANGE_ACTION_DELETE:
|
||||||
model_deletes.labels(obj._meta.model_name).inc()
|
model_deletes.labels(obj._meta.model_name).inc()
|
||||||
|
|
||||||
# Housekeeping: 1% chance of clearing out expired ObjectChanges
|
# Housekeeping: 1% chance of clearing out expired ObjectChanges. This applies only to requests which result in
|
||||||
if _thread_locals.changed_objects and settings.CHANGELOG_RETENTION and random.randint(1, 100) == 1:
|
# one or more changes being logged.
|
||||||
|
if settings.CHANGELOG_RETENTION and random.randint(1, 100) == 1:
|
||||||
cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION)
|
cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION)
|
||||||
purged_count, _ = ObjectChange.objects.filter(
|
purged_count, _ = ObjectChange.objects.filter(
|
||||||
time__lt=cutoff
|
time__lt=cutoff
|
||||||
|
Loading…
Reference in New Issue
Block a user