mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Remove old enqueue_job() method
This commit is contained in:
parent
69fd138533
commit
d752d664f3
@ -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):
|
||||
"""
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
)
|
||||
|
||||
|
@ -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()
|
||||
)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user