Remove old enqueue_job() method

This commit is contained in:
jeremystretch 2023-03-27 16:56:43 -04:00
parent 69fd138533
commit d752d664f3
7 changed files with 31 additions and 78 deletions

View File

@ -161,39 +161,6 @@ class Job(models.Model):
# Handle webhooks # Handle webhooks
self.trigger_webhooks(event=EVENT_JOB_END) 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 @classmethod
def enqueue(cls, func, instance, name=None, user=None, schedule_at=None, interval=None, **kwargs): def enqueue(cls, func, instance, name=None, user=None, schedule_at=None, interval=None, **kwargs):
""" """

View File

@ -17,8 +17,8 @@ from core.choices import JobStatusChoices
from core.models import Job from core.models import Job
from extras import filtersets from extras import filtersets
from extras.models import * from extras.models import *
from extras.reports import get_module_and_report, get_report, run_report from extras.reports import get_module_and_report, run_report
from extras.scripts import get_module_and_script, get_script, run_script from extras.scripts import get_module_and_script, run_script
from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
from netbox.api.features import SyncedDataMixin from netbox.api.features import SyncedDataMixin
from netbox.api.metadata import ContentTypeMetadata from netbox.api.metadata import ContentTypeMetadata

View File

@ -1,6 +1,5 @@
import time import time
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.utils import timezone from django.utils import timezone
@ -27,12 +26,10 @@ class Command(BaseCommand):
"[{:%H:%M:%S}] Running {}...".format(timezone.now(), report.full_name) "[{:%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.enqueue_job(
run_report, run_report,
report.full_name, instance=module,
report_content_type, name=report.class_name,
None,
job_timeout=report.job_timeout job_timeout=report.job_timeout
) )

View File

@ -5,7 +5,6 @@ import traceback
import uuid import uuid
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.db import transaction from django.db import transaction
@ -13,7 +12,7 @@ from core.choices import JobStatusChoices
from core.models import Job from core.models import Job
from extras.api.serializers import ScriptOutputSerializer from extras.api.serializers import ScriptOutputSerializer
from extras.context_managers import change_logging 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 extras.signals import clear_webhooks
from utilities.exceptions import AbortTransaction from utilities.exceptions import AbortTransaction
from utilities.utils import NetBoxFakeRequest from utilities.utils import NetBoxFakeRequest
@ -73,7 +72,8 @@ class Command(BaseCommand):
except TypeError: except TypeError:
data = {} 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 # Take user from command line if provided and exists, other
if options['user']: if options['user']:
@ -90,7 +90,7 @@ class Command(BaseCommand):
stdouthandler.setLevel(logging.DEBUG) stdouthandler.setLevel(logging.DEBUG)
stdouthandler.setFormatter(formatter) stdouthandler.setFormatter(formatter)
logger = logging.getLogger(f"netbox.scripts.{module}.{name}") logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
logger.addHandler(stdouthandler) logger.addHandler(stdouthandler)
try: try:
@ -105,17 +105,14 @@ class Command(BaseCommand):
except KeyError: except KeyError:
raise CommandError(f"Invalid log level: {loglevel}") raise CommandError(f"Invalid log level: {loglevel}")
# Get the script # Initialize the script form
script = get_script(module, name)() script = script()
# Parse the parameters
form = script.as_form(data, None) form = script.as_form(data, None)
script_content_type = ContentType.objects.get(app_label='extras', model='script') # Create the job
# Create the job result
job_result = Job.objects.create( job_result = Job.objects.create(
name=script.full_name, instance=module,
obj_type=script_content_type, name=script.name,
user=User.objects.filter(is_superuser=True).order_by('pk')[0], user=User.objects.filter(is_superuser=True).order_by('pk')[0],
job_id=uuid.uuid4() job_id=uuid.uuid4()
) )

View File

@ -11,17 +11,15 @@ from core.models import Job
from .choices import LogLevelChoices from .choices import LogLevelChoices
from .models import ReportModule from .models import ReportModule
__all__ = (
'Report',
'get_module_and_report',
'run_report',
)
logger = logging.getLogger(__name__) 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): def get_module_and_report(module_name, report_name):
module = ReportModule.objects.get(file_path=f'{module_name}.py') module = ReportModule.objects.get(file_path=f'{module_name}.py')
report = module.reports.get(report_name) report = module.reports.get(report_name)

View File

@ -25,7 +25,7 @@ from utilities.forms import add_blank_choice, DynamicModelChoiceField, DynamicMo
from .context_managers import change_logging from .context_managers import change_logging
from .forms import ScriptForm from .forms import ScriptForm
__all__ = [ __all__ = (
'BaseScript', 'BaseScript',
'BooleanVar', 'BooleanVar',
'ChoiceVar', 'ChoiceVar',
@ -40,7 +40,9 @@ __all__ = [
'Script', 'Script',
'StringVar', 'StringVar',
'TextVar', 'TextVar',
] 'get_module_and_script',
'run_script',
)
# #
@ -436,6 +438,12 @@ def is_variable(obj):
return isinstance(obj, ScriptVariable) 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): 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 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, request=request,
commit=commit 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

View File

@ -22,7 +22,7 @@ from utilities.views import ContentTypePermissionRequiredMixin, register_model_v
from . import filtersets, forms, tables from . import filtersets, forms, tables
from .forms.reports import ReportForm from .forms.reports import ReportForm
from .models import * from .models import *
from .reports import get_report, run_report from .reports import run_report
from .scripts import run_script from .scripts import run_script