From 080a001118e43669fbce56a66c88e30712d1c424 Mon Sep 17 00:00:00 2001 From: kkthxbye-code Date: Mon, 5 Dec 2022 10:04:28 +0100 Subject: [PATCH] Allow redefining internally used queues --- docs/configuration/miscellaneous.md | 16 ++++++++++++++++ netbox/extras/models/models.py | 4 +++- netbox/extras/webhooks.py | 4 +++- netbox/netbox/settings.py | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/configuration/miscellaneous.md b/docs/configuration/miscellaneous.md index 614e90eac..4eb090554 100644 --- a/docs/configuration/miscellaneous.md +++ b/docs/configuration/miscellaneous.md @@ -141,6 +141,22 @@ When determining the primary IP address for a device, IPv6 is preferred over IPv --- +## QUEUE_MAPPINGS + +Allows changing which queues are used internally for background tasks. + +```python +QUEUE_MAPPINGS = { + 'webhook': 'low', + 'report': 'high', + 'script': 'high', +} +``` + +If no queue is defined the queue named `default` will be used. + +--- + ## RELEASE_CHECK_URL Default: None (disabled) diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 95a414225..402047194 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -21,6 +21,7 @@ from extras.choices import * from extras.constants import * from extras.conditions import ConditionSet from extras.utils import FeatureQuery, image_upload +from netbox.config import get_config from netbox.models import ChangeLoggedModel from netbox.models.features import ( CloningMixin, CustomFieldsMixin, CustomLinksMixin, ExportTemplatesMixin, JobResultsMixin, TagsMixin, WebhooksMixin, @@ -681,7 +682,8 @@ class JobResult(models.Model): job_id=uuid.uuid4() ) - queue = django_rq.get_queue("default") + rq_queue_name = get_config().QUEUE_MAPPINGS.get(obj_type.name, 'default') + queue = django_rq.get_queue(rq_queue_name) if schedule_at: job_result.status = JobResultStatusChoices.STATUS_SCHEDULED diff --git a/netbox/extras/webhooks.py b/netbox/extras/webhooks.py index a93be7934..18dcd0cfe 100644 --- a/netbox/extras/webhooks.py +++ b/netbox/extras/webhooks.py @@ -5,6 +5,7 @@ from django.contrib.contenttypes.models import ContentType from django.utils import timezone from django_rq import get_queue +from netbox.config import get_config from netbox.registry import registry from utilities.api import get_serializer_for_model from utilities.utils import serialize_object @@ -78,7 +79,8 @@ def flush_webhooks(queue): """ Flush a list of object representation to RQ for webhook processing. """ - rq_queue = get_queue('default') + rq_queue_name = get_config().QUEUE_MAPPINGS.get('webhook', 'default') + rq_queue = get_queue(rq_queue_name) webhooks_cache = { 'type_create': {}, 'type_update': {}, diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 4dbf48c36..15017f966 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -101,6 +101,7 @@ MEDIA_ROOT = getattr(configuration, 'MEDIA_ROOT', os.path.join(BASE_DIR, 'media' METRICS_ENABLED = getattr(configuration, 'METRICS_ENABLED', False) PLUGINS = getattr(configuration, 'PLUGINS', []) PLUGINS_CONFIG = getattr(configuration, 'PLUGINS_CONFIG', {}) +QUEUE_MAPPINGS = getattr(configuration, 'QUEUE_MAPPINGS', {}) RELEASE_CHECK_URL = getattr(configuration, 'RELEASE_CHECK_URL', None) REMOTE_AUTH_AUTO_CREATE_USER = getattr(configuration, 'REMOTE_AUTH_AUTO_CREATE_USER', False) REMOTE_AUTH_BACKEND = getattr(configuration, 'REMOTE_AUTH_BACKEND', 'netbox.authentication.RemoteUserBackend')