From 40eec679d99493bcbc0268571af12eabab246473 Mon Sep 17 00:00:00 2001 From: bctiemann Date: Thu, 2 Apr 2026 17:09:53 -0400 Subject: [PATCH] Fixes: #21696 - Upgrade to django-rq==4.0.1 (#21805) --- netbox/core/api/views.py | 8 ++++---- netbox/core/tests/test_views.py | 16 ++++++++-------- netbox/core/utils.py | 20 ++++++++++---------- netbox/core/views.py | 8 ++++---- netbox/netbox/configuration_testing.py | 4 ++++ netbox/netbox/settings.py | 1 + requirements.txt | 2 +- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/netbox/core/api/views.py b/netbox/core/api/views.py index bea332f1b..7d5e21f20 100644 --- a/netbox/core/api/views.py +++ b/netbox/core/api/views.py @@ -2,7 +2,7 @@ from django.http import Http404, HttpResponse from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ from django_rq.queues import get_redis_connection -from django_rq.settings import QUEUES_LIST +from django_rq.settings import get_queues_list from django_rq.utils import get_statistics from drf_spectacular.types import OpenApiTypes from drf_spectacular.utils import OpenApiParameter, extend_schema @@ -195,7 +195,7 @@ class BackgroundWorkerViewSet(BaseRQViewSet): return 'Background Workers' def get_data(self): - config = QUEUES_LIST[0] + config = get_queues_list()[0] return Worker.all(get_redis_connection(config['connection_config'])) @extend_schema( @@ -205,7 +205,7 @@ class BackgroundWorkerViewSet(BaseRQViewSet): ) def retrieve(self, request, name): # all the RQ queues should use the same connection - config = QUEUES_LIST[0] + config = get_queues_list()[0] workers = Worker.all(get_redis_connection(config['connection_config'])) worker = next((item for item in workers if item.name == name), None) if not worker: @@ -229,7 +229,7 @@ class BackgroundTaskViewSet(BaseRQViewSet): return get_rq_jobs() def get_task_from_id(self, task_id): - config = QUEUES_LIST[0] + config = get_queues_list()[0] task = RQ_Job.fetch(task_id, connection=get_redis_connection(config['connection_config'])) if not task: raise Http404 diff --git a/netbox/core/tests/test_views.py b/netbox/core/tests/test_views.py index f4254a299..82838a576 100644 --- a/netbox/core/tests/test_views.py +++ b/netbox/core/tests/test_views.py @@ -6,7 +6,7 @@ from datetime import datetime from django.urls import reverse from django.utils import timezone from django_rq import get_queue -from django_rq.settings import QUEUES_MAP +from django_rq.settings import get_queues_map from django_rq.workers import get_worker from rq.job import Job as RQ_Job from rq.job import JobStatus @@ -189,7 +189,7 @@ class BackgroundTaskTestCase(TestCase): def test_background_tasks_list_default(self): queue = get_queue('default') queue.enqueue(self.dummy_job_default) - queue_index = QUEUES_MAP['default'] + queue_index = get_queues_map()['default'] response = self.client.get(reverse('core:background_task_list', args=[queue_index, 'queued'])) self.assertEqual(response.status_code, 200) @@ -198,7 +198,7 @@ class BackgroundTaskTestCase(TestCase): def test_background_tasks_list_high(self): queue = get_queue('high') queue.enqueue(self.dummy_job_high) - queue_index = QUEUES_MAP['high'] + queue_index = get_queues_map()['high'] response = self.client.get(reverse('core:background_task_list', args=[queue_index, 'queued'])) self.assertEqual(response.status_code, 200) @@ -207,7 +207,7 @@ class BackgroundTaskTestCase(TestCase): def test_background_tasks_list_finished(self): queue = get_queue('default') job = queue.enqueue(self.dummy_job_default) - queue_index = QUEUES_MAP['default'] + queue_index = get_queues_map()['default'] registry = FinishedJobRegistry(queue.name, queue.connection) registry.add(job, 2) @@ -218,7 +218,7 @@ class BackgroundTaskTestCase(TestCase): def test_background_tasks_list_failed(self): queue = get_queue('default') job = queue.enqueue(self.dummy_job_default) - queue_index = QUEUES_MAP['default'] + queue_index = get_queues_map()['default'] registry = FailedJobRegistry(queue.name, queue.connection) registry.add(job, 2) @@ -229,7 +229,7 @@ class BackgroundTaskTestCase(TestCase): def test_background_tasks_scheduled(self): queue = get_queue('default') queue.enqueue_at(datetime.now(), self.dummy_job_default) - queue_index = QUEUES_MAP['default'] + queue_index = get_queues_map()['default'] response = self.client.get(reverse('core:background_task_list', args=[queue_index, 'scheduled'])) self.assertEqual(response.status_code, 200) @@ -238,7 +238,7 @@ class BackgroundTaskTestCase(TestCase): def test_background_tasks_list_deferred(self): queue = get_queue('default') job = queue.enqueue(self.dummy_job_default) - queue_index = QUEUES_MAP['default'] + queue_index = get_queues_map()['default'] registry = DeferredJobRegistry(queue.name, queue.connection) registry.add(job, 2) @@ -335,7 +335,7 @@ class BackgroundTaskTestCase(TestCase): worker2 = get_worker('high') worker2.register_birth() - queue_index = QUEUES_MAP['default'] + queue_index = get_queues_map()['default'] response = self.client.get(reverse('core:worker_list', args=[queue_index])) self.assertEqual(response.status_code, 200) self.assertIn(str(worker1.name), str(response.content)) diff --git a/netbox/core/utils.py b/netbox/core/utils.py index d5be09b49..1ef6e5136 100644 --- a/netbox/core/utils.py +++ b/netbox/core/utils.py @@ -1,7 +1,7 @@ from django.http import Http404 from django.utils.translation import gettext_lazy as _ from django_rq.queues import get_queue, get_queue_by_index, get_redis_connection -from django_rq.settings import QUEUES_LIST, QUEUES_MAP +from django_rq.settings import get_queues_list, get_queues_map from django_rq.utils import get_jobs, stop_jobs from rq import requeue_job from rq.exceptions import NoSuchJobError @@ -31,7 +31,7 @@ def get_rq_jobs(): """ jobs = set() - for queue in QUEUES_LIST: + for queue in get_queues_list(): queue = get_queue(queue['name']) jobs.update(queue.get_jobs()) @@ -78,13 +78,13 @@ def delete_rq_job(job_id): """ Delete the specified RQ job. """ - config = QUEUES_LIST[0] + config = get_queues_list()[0] try: job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),) except NoSuchJobError: raise Http404(_("Job {job_id} not found").format(job_id=job_id)) - queue_index = QUEUES_MAP[job.origin] + queue_index = get_queues_map()[job.origin] queue = get_queue_by_index(queue_index) # Remove job id from queue and delete the actual job @@ -96,13 +96,13 @@ def requeue_rq_job(job_id): """ Requeue the specified RQ job. """ - config = QUEUES_LIST[0] + config = get_queues_list()[0] try: job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),) except NoSuchJobError: raise Http404(_("Job {id} not found.").format(id=job_id)) - queue_index = QUEUES_MAP[job.origin] + queue_index = get_queues_map()[job.origin] queue = get_queue_by_index(queue_index) requeue_job(job_id, connection=queue.connection, serializer=queue.serializer) @@ -112,13 +112,13 @@ def enqueue_rq_job(job_id): """ Enqueue the specified RQ job. """ - config = QUEUES_LIST[0] + config = get_queues_list()[0] try: job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),) except NoSuchJobError: raise Http404(_("Job {id} not found.").format(id=job_id)) - queue_index = QUEUES_MAP[job.origin] + queue_index = get_queues_map()[job.origin] queue = get_queue_by_index(queue_index) try: @@ -144,13 +144,13 @@ def stop_rq_job(job_id): """ Stop the specified RQ job. """ - config = QUEUES_LIST[0] + config = get_queues_list()[0] try: job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),) except NoSuchJobError: raise Http404(_("Job {job_id} not found").format(job_id=job_id)) - queue_index = QUEUES_MAP[job.origin] + queue_index = get_queues_map()[job.origin] queue = get_queue_by_index(queue_index) return stop_jobs(queue, job_id)[0] diff --git a/netbox/core/views.py b/netbox/core/views.py index 780e0957c..9fcfdf728 100644 --- a/netbox/core/views.py +++ b/netbox/core/views.py @@ -14,7 +14,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django.views.generic import View from django_rq.queues import get_connection, get_queue_by_index, get_redis_connection -from django_rq.settings import QUEUES_LIST, QUEUES_MAP +from django_rq.settings import get_queues_list, get_queues_map from django_rq.utils import get_statistics from rq.exceptions import NoSuchJobError from rq.job import Job as RQ_Job @@ -524,13 +524,13 @@ class BackgroundTaskView(BaseRQView): def get(self, request, job_id): # all the RQ queues should use the same connection - config = QUEUES_LIST[0] + config = get_queues_list()[0] try: job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),) except NoSuchJobError: raise Http404(_("Job {job_id} not found").format(job_id=job_id)) - queue_index = QUEUES_MAP[job.origin] + queue_index = get_queues_map()[job.origin] queue = get_queue_by_index(queue_index) try: @@ -640,7 +640,7 @@ class WorkerView(BaseRQView): def get(self, request, key): # all the RQ queues should use the same connection - config = QUEUES_LIST[0] + config = get_queues_list()[0] worker = Worker.find_by_key('rq:worker:' + key, connection=get_redis_connection(config['connection_config'])) # Convert microseconds to milliseconds worker.total_working_time = worker.total_working_time / 1000 diff --git a/netbox/netbox/configuration_testing.py b/netbox/netbox/configuration_testing.py index 3e552e944..3ac9c2642 100644 --- a/netbox/netbox/configuration_testing.py +++ b/netbox/netbox/configuration_testing.py @@ -20,6 +20,10 @@ PLUGINS = [ 'netbox.tests.dummy_plugin', ] +RQ = { + 'COMMIT_MODE': 'auto', +} + REDIS = { 'tasks': { 'HOST': 'localhost', diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 90c9f8334..0840f0149 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -168,6 +168,7 @@ REMOTE_AUTH_USER_FIRST_NAME = getattr(configuration, 'REMOTE_AUTH_USER_FIRST_NAM REMOTE_AUTH_USER_LAST_NAME = getattr(configuration, 'REMOTE_AUTH_USER_LAST_NAME', 'HTTP_REMOTE_USER_LAST_NAME') # Required by extras/migrations/0109_script_models.py REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/') +RQ = getattr(configuration, 'RQ', {}) RQ_DEFAULT_TIMEOUT = getattr(configuration, 'RQ_DEFAULT_TIMEOUT', 300) RQ_RETRY_INTERVAL = getattr(configuration, 'RQ_RETRY_INTERVAL', 60) RQ_RETRY_MAX = getattr(configuration, 'RQ_RETRY_MAX', 0) diff --git a/requirements.txt b/requirements.txt index 89c0590f2..c6fdbb5c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,7 @@ django-pglocks==1.0.4 django-prometheus==2.4.1 django-redis==6.0.0 django-rich==2.2.0 -django-rq==3.2.2 +django-rq==4.0.1 django-storages==1.14.6 django-tables2==2.8.0 django-taggit==6.1.0