From d752d664f3e23992b6166f2315b1f60a9bb49cd1 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 27 Mar 2023 16:56:43 -0400 Subject: [PATCH] Remove old enqueue_job() method --- netbox/core/models/jobs.py | 33 ------------------- netbox/extras/api/views.py | 4 +-- .../extras/management/commands/runreport.py | 9 ++--- .../extras/management/commands/runscript.py | 21 +++++------- netbox/extras/reports.py | 14 ++++---- netbox/extras/scripts.py | 26 ++++++--------- netbox/extras/views.py | 2 +- 7 files changed, 31 insertions(+), 78 deletions(-) diff --git a/netbox/core/models/jobs.py b/netbox/core/models/jobs.py index cd2ac8ab5..24257d3fb 100644 --- a/netbox/core/models/jobs.py +++ b/netbox/core/models/jobs.py @@ -161,39 +161,6 @@ class Job(models.Model): # Handle webhooks self.trigger_webhooks(event=EVENT_JOB_END) - @classmethod - def enqueue_job(cls, func, name, obj_type, user, schedule_at=None, interval=None, *args, **kwargs): - """ - Create a Job instance and enqueue a job using the given callable - - Args: - func: The callable object to be enqueued for execution - name: Name for the job (optional) - obj_type: ContentType to link to the Job instance object_type - user: User object to link to the Job instance - schedule_at: Schedule the job to be executed at the passed date and time - interval: Recurrence interval (in minutes) - """ - rq_queue_name = get_queue_for_model(obj_type.model) - queue = django_rq.get_queue(rq_queue_name) - status = JobStatusChoices.STATUS_SCHEDULED if schedule_at else JobStatusChoices.STATUS_PENDING - job = Job.objects.create( - name=name, - status=status, - object_type=obj_type, - scheduled=schedule_at, - interval=interval, - user=user, - job_id=uuid.uuid4() - ) - - if schedule_at: - queue.enqueue_at(schedule_at, func, job_id=str(job.job_id), job_result=job, **kwargs) - else: - queue.enqueue(func, job_id=str(job.job_id), job_result=job, **kwargs) - - return job - @classmethod def enqueue(cls, func, instance, name=None, user=None, schedule_at=None, interval=None, **kwargs): """ diff --git a/netbox/extras/api/views.py b/netbox/extras/api/views.py index e0dc6b100..65f74e2e2 100644 --- a/netbox/extras/api/views.py +++ b/netbox/extras/api/views.py @@ -17,8 +17,8 @@ from core.choices import JobStatusChoices from core.models import Job from extras import filtersets from extras.models import * -from extras.reports import get_module_and_report, get_report, run_report -from extras.scripts import get_module_and_script, get_script, run_script +from extras.reports import get_module_and_report, run_report +from extras.scripts import get_module_and_script, run_script from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired from netbox.api.features import SyncedDataMixin from netbox.api.metadata import ContentTypeMetadata diff --git a/netbox/extras/management/commands/runreport.py b/netbox/extras/management/commands/runreport.py index 6ce1251ad..808e07cdf 100644 --- a/netbox/extras/management/commands/runreport.py +++ b/netbox/extras/management/commands/runreport.py @@ -1,6 +1,5 @@ import time -from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand from django.utils import timezone @@ -27,12 +26,10 @@ class Command(BaseCommand): "[{:%H:%M:%S}] Running {}...".format(timezone.now(), report.full_name) ) - report_content_type = ContentType.objects.get(app_label='extras', model='report') - job = Job.enqueue_job( + job = Job.enqueue( run_report, - report.full_name, - report_content_type, - None, + instance=module, + name=report.class_name, job_timeout=report.job_timeout ) diff --git a/netbox/extras/management/commands/runscript.py b/netbox/extras/management/commands/runscript.py index 597fe05a7..34d4d160d 100644 --- a/netbox/extras/management/commands/runscript.py +++ b/netbox/extras/management/commands/runscript.py @@ -5,7 +5,6 @@ import traceback import uuid from django.contrib.auth.models import User -from django.contrib.contenttypes.models import ContentType from django.core.management.base import BaseCommand, CommandError from django.db import transaction @@ -13,7 +12,7 @@ from core.choices import JobStatusChoices from core.models import Job from extras.api.serializers import ScriptOutputSerializer from extras.context_managers import change_logging -from extras.scripts import get_script +from extras.scripts import get_module_and_script from extras.signals import clear_webhooks from utilities.exceptions import AbortTransaction from utilities.utils import NetBoxFakeRequest @@ -73,7 +72,8 @@ class Command(BaseCommand): except TypeError: data = {} - module, name = script.split('.', 1) + module_name, script_name = script.split('.', 1) + module, script = get_module_and_script(module_name, script_name) # Take user from command line if provided and exists, other if options['user']: @@ -90,7 +90,7 @@ class Command(BaseCommand): stdouthandler.setLevel(logging.DEBUG) stdouthandler.setFormatter(formatter) - logger = logging.getLogger(f"netbox.scripts.{module}.{name}") + logger = logging.getLogger(f"netbox.scripts.{script.full_name}") logger.addHandler(stdouthandler) try: @@ -105,17 +105,14 @@ class Command(BaseCommand): except KeyError: raise CommandError(f"Invalid log level: {loglevel}") - # Get the script - script = get_script(module, name)() - # Parse the parameters + # Initialize the script form + script = script() form = script.as_form(data, None) - script_content_type = ContentType.objects.get(app_label='extras', model='script') - - # Create the job result + # Create the job job_result = Job.objects.create( - name=script.full_name, - obj_type=script_content_type, + instance=module, + name=script.name, user=User.objects.filter(is_superuser=True).order_by('pk')[0], job_id=uuid.uuid4() ) diff --git a/netbox/extras/reports.py b/netbox/extras/reports.py index 4dd62ed29..3bb2cae0f 100644 --- a/netbox/extras/reports.py +++ b/netbox/extras/reports.py @@ -11,17 +11,15 @@ from core.models import Job from .choices import LogLevelChoices from .models import ReportModule +__all__ = ( + 'Report', + 'get_module_and_report', + 'run_report', +) + logger = logging.getLogger(__name__) -def get_report(module_name, report_name): - """ - Return a specific report from within a module. - """ - module = ReportModule.objects.get(file_path=f'{module_name}.py') - return module.reports.get(report_name) - - def get_module_and_report(module_name, report_name): module = ReportModule.objects.get(file_path=f'{module_name}.py') report = module.reports.get(report_name) diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index 9526e1cf8..5b9a351ec 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -25,7 +25,7 @@ from utilities.forms import add_blank_choice, DynamicModelChoiceField, DynamicMo from .context_managers import change_logging from .forms import ScriptForm -__all__ = [ +__all__ = ( 'BaseScript', 'BooleanVar', 'ChoiceVar', @@ -40,7 +40,9 @@ __all__ = [ 'Script', 'StringVar', 'TextVar', -] + 'get_module_and_script', + 'run_script', +) # @@ -436,6 +438,12 @@ def is_variable(obj): return isinstance(obj, ScriptVariable) +def get_module_and_script(module_name, script_name): + module = ScriptModule.objects.get(file_path=f'{module_name}.py') + script = module.scripts.get(script_name) + return module, script + + def run_script(data, request, commit=True, *args, **kwargs): """ A wrapper for calling Script.run(). This performs error handling and provides a hook for committing changes. It @@ -512,17 +520,3 @@ def run_script(data, request, commit=True, *args, **kwargs): request=request, commit=commit ) - - -def get_script(module_name, script_name): - """ - Retrieve a script class by module and name. Returns None if the script does not exist. - """ - module = ScriptModule.objects.get(file_path=f'{module_name}.py') - return module.scripts.get(script_name) - - -def get_module_and_script(module_name, script_name): - module = ScriptModule.objects.get(file_path=f'{module_name}.py') - script = module.scripts.get(script_name) - return module, script diff --git a/netbox/extras/views.py b/netbox/extras/views.py index c4640b890..7aa908803 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -22,7 +22,7 @@ from utilities.views import ContentTypePermissionRequiredMixin, register_model_v from . import filtersets, forms, tables from .forms.reports import ReportForm from .models import * -from .reports import get_report, run_report +from .reports import run_report from .scripts import run_script