From 4b3b88f23d7dd950e40a72d526fef0b9efeae0eb Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Oct 2023 14:55:16 -0700 Subject: [PATCH] 14132 webhooks_queue -> events_queue --- netbox/extras/context_managers.py | 8 ++++---- netbox/extras/signals.py | 14 +++++++------- netbox/extras/tests/test_webhooks.py | 6 +++--- netbox/netbox/context.py | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/netbox/extras/context_managers.py b/netbox/extras/context_managers.py index 392d19c5d..e0af2742a 100644 --- a/netbox/extras/context_managers.py +++ b/netbox/extras/context_managers.py @@ -1,6 +1,6 @@ from contextlib import contextmanager -from netbox.context import current_request, webhooks_queue +from netbox.context import current_request, events_queue from .webhooks import flush_webhooks @@ -13,13 +13,13 @@ def event_wrapper(request): :param request: WSGIRequest object with a unique `id` set """ current_request.set(request) - webhooks_queue.set([]) + events_queue.set([]) yield # Flush queued webhooks to RQ - flush_webhooks(webhooks_queue.get()) + flush_webhooks(events_queue.get()) # Clear context vars current_request.set(None) - webhooks_queue.set([]) + events_queue.set([]) diff --git a/netbox/extras/signals.py b/netbox/extras/signals.py index 8bdaf523c..86d88b6c3 100644 --- a/netbox/extras/signals.py +++ b/netbox/extras/signals.py @@ -10,7 +10,7 @@ from django_prometheus.models import model_deletes, model_inserts, model_updates from extras.validators import CustomValidator from netbox.config import get_config -from netbox.context import current_request, webhooks_queue +from netbox.context import current_request, events_queue from netbox.signals import post_clean from utilities.exceptions import AbortRequest from .choices import ObjectChangeActionChoices @@ -81,14 +81,14 @@ def handle_changed_object(sender, instance, **kwargs): objectchange.save() # If this is an M2M change, update the previously queued webhook (from post_save) - queue = webhooks_queue.get() + queue = events_queue.get() if m2m_changed and queue and is_same_object(instance, queue[-1], request.id): instance.refresh_from_db() # Ensure that we're working with fresh M2M assignments queue[-1]['data'] = serialize_for_webhook(instance) queue[-1]['snapshots']['postchange'] = get_snapshots(instance, action)['postchange'] else: enqueue_object(queue, instance, request.user, request.id, action) - webhooks_queue.set(queue) + events_queue.set(queue) # Increment metric counters if action == ObjectChangeActionChoices.ACTION_CREATE: @@ -117,9 +117,9 @@ def handle_deleted_object(sender, instance, **kwargs): objectchange.save() # Enqueue webhooks - queue = webhooks_queue.get() + queue = events_queue.get() enqueue_object(queue, instance, request.user, request.id, ObjectChangeActionChoices.ACTION_DELETE) - webhooks_queue.set(queue) + events_queue.set(queue) # Increment metric counters model_deletes.labels(instance._meta.model_name).inc() @@ -131,8 +131,8 @@ def clear_webhook_queue(sender, **kwargs): Delete any queued webhooks (e.g. because of an aborted bulk transaction) """ logger = logging.getLogger('webhooks') - logger.info(f"Clearing {len(webhooks_queue.get())} queued webhooks ({sender})") - webhooks_queue.set([]) + logger.info(f"Clearing {len(events_queue.get())} queued webhooks ({sender})") + events_queue.set([]) # diff --git a/netbox/extras/tests/test_webhooks.py b/netbox/extras/tests/test_webhooks.py index ef7637765..e8cc51b8e 100644 --- a/netbox/extras/tests/test_webhooks.py +++ b/netbox/extras/tests/test_webhooks.py @@ -313,16 +313,16 @@ class WebhookTest(APITestCase): return HttpResponse() # Enqueue a webhook for processing - webhooks_queue = [] + events_queue = [] site = Site.objects.create(name='Site 1', slug='site-1') enqueue_object( - webhooks_queue, + events_queue, instance=site, user=self.user, request_id=request_id, action=ObjectChangeActionChoices.ACTION_CREATE ) - flush_webhooks(webhooks_queue) + flush_webhooks(events_queue) # Retrieve the job from queue job = self.queue.jobs[0] diff --git a/netbox/netbox/context.py b/netbox/netbox/context.py index 5461a4e94..56e41cb63 100644 --- a/netbox/netbox/context.py +++ b/netbox/netbox/context.py @@ -2,9 +2,9 @@ from contextvars import ContextVar __all__ = ( 'current_request', - 'webhooks_queue', + 'events_queue', ) current_request = ContextVar('current_request', default=None) -webhooks_queue = ContextVar('webhooks_queue', default=[]) +events_queue = ContextVar('events_queue', default=[])