mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Clean up old naming
This commit is contained in:
parent
4449003b59
commit
c10793176f
@ -8,22 +8,22 @@ from .models import DataSource
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def sync_datasource(job_result, *args, **kwargs):
|
||||
def sync_datasource(job, *args, **kwargs):
|
||||
"""
|
||||
Call sync() on a DataSource.
|
||||
"""
|
||||
datasource = DataSource.objects.get(pk=job_result.object_id)
|
||||
datasource = DataSource.objects.get(pk=job.object_id)
|
||||
|
||||
try:
|
||||
job_result.start()
|
||||
job.start()
|
||||
datasource.sync()
|
||||
|
||||
# Update the search cache for DataFiles belonging to this source
|
||||
search_backend.cache(datasource.datafiles.iterator())
|
||||
|
||||
job_result.terminate()
|
||||
job.terminate()
|
||||
|
||||
except SyncError as e:
|
||||
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
DataSource.objects.filter(pk=datasource.pk).update(status=DataSourceStatusChoices.FAILED)
|
||||
logging.error(e)
|
||||
|
@ -119,14 +119,12 @@ class DataSource(JobsMixin, PrimaryModel):
|
||||
DataSource.objects.filter(pk=self.pk).update(status=self.status)
|
||||
|
||||
# Enqueue a sync job
|
||||
job_result = Job.enqueue(
|
||||
return Job.enqueue(
|
||||
import_string('core.jobs.sync_datasource'),
|
||||
instance=self,
|
||||
user=request.user
|
||||
)
|
||||
|
||||
return job_result
|
||||
|
||||
def get_backend(self):
|
||||
backend_cls = registry['data_backends'].get(self.type)
|
||||
backend_params = self.parameters or {}
|
||||
|
@ -190,9 +190,9 @@ class Job(models.Model):
|
||||
)
|
||||
|
||||
if schedule_at:
|
||||
queue.enqueue_at(schedule_at, func, job_id=str(job.job_id), job_result=job, **kwargs)
|
||||
queue.enqueue_at(schedule_at, func, job_id=str(job.job_id), job=job, **kwargs)
|
||||
else:
|
||||
queue.enqueue(func, job_id=str(job.job_id), job_result=job, **kwargs)
|
||||
queue.enqueue(func, job_id=str(job.job_id), job=job, **kwargs)
|
||||
|
||||
return job
|
||||
|
||||
|
@ -55,9 +55,9 @@ class DataSourceSyncView(BaseObjectView):
|
||||
|
||||
def post(self, request, pk):
|
||||
datasource = get_object_or_404(self.queryset, pk=pk)
|
||||
job_result = datasource.enqueue_sync_job(request)
|
||||
job = datasource.enqueue_sync_job(request)
|
||||
|
||||
messages.success(request, f"Queued job #{job_result.pk} to sync {datasource}")
|
||||
messages.success(request, f"Queued job #{job.pk} to sync {datasource}")
|
||||
return redirect(datasource.get_absolute_url())
|
||||
|
||||
|
||||
|
@ -48,8 +48,8 @@ class Command(BaseCommand):
|
||||
except AbortTransaction:
|
||||
script.log_info("Database changes have been reverted automatically.")
|
||||
clear_webhooks.send(request)
|
||||
job_result.data = ScriptOutputSerializer(script).data
|
||||
job_result.terminate()
|
||||
job.data = ScriptOutputSerializer(script).data
|
||||
job.terminate()
|
||||
except Exception as e:
|
||||
stacktrace = traceback.format_exc()
|
||||
script.log_failure(
|
||||
@ -58,10 +58,10 @@ class Command(BaseCommand):
|
||||
script.log_info("Database changes have been reverted due to error.")
|
||||
logger.error(f"Exception raised during script execution: {e}")
|
||||
clear_webhooks.send(request)
|
||||
job_result.data = ScriptOutputSerializer(script).data
|
||||
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
job.data = ScriptOutputSerializer(script).data
|
||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
|
||||
logger.info(f"Script completed in {job_result.duration}")
|
||||
logger.info(f"Script completed in {job.duration}")
|
||||
|
||||
# Params
|
||||
script = options['script']
|
||||
@ -110,7 +110,7 @@ class Command(BaseCommand):
|
||||
form = script.as_form(data, None)
|
||||
|
||||
# Create the job
|
||||
job_result = Job.objects.create(
|
||||
job = Job.objects.create(
|
||||
instance=module,
|
||||
name=script.name,
|
||||
user=User.objects.filter(is_superuser=True).order_by('pk')[0],
|
||||
@ -124,12 +124,12 @@ class Command(BaseCommand):
|
||||
'FILES': {},
|
||||
'user': user,
|
||||
'path': '',
|
||||
'id': job_result.job_id
|
||||
'id': job.job_id
|
||||
})
|
||||
|
||||
if form.is_valid():
|
||||
job_result.status = JobStatusChoices.STATUS_RUNNING
|
||||
job_result.save()
|
||||
job.status = JobStatusChoices.STATUS_RUNNING
|
||||
job.save()
|
||||
|
||||
logger.info(f"Running script (commit={commit})")
|
||||
script.request = request
|
||||
@ -143,5 +143,5 @@ class Command(BaseCommand):
|
||||
for field, errors in form.errors.get_json_data().items():
|
||||
for error in errors:
|
||||
logger.error(f'\t{field}: {error.get("message")}')
|
||||
job_result.status = JobStatusChoices.STATUS_ERRORED
|
||||
job_result.save()
|
||||
job.status = JobStatusChoices.STATUS_ERRORED
|
||||
job.save()
|
||||
|
@ -27,33 +27,33 @@ def get_module_and_report(module_name, report_name):
|
||||
|
||||
|
||||
@job('default')
|
||||
def run_report(job_result, *args, **kwargs):
|
||||
def run_report(job, *args, **kwargs):
|
||||
"""
|
||||
Helper function to call the run method on a report. This is needed to get around the inability to pickle an instance
|
||||
method for queueing into the background processor.
|
||||
"""
|
||||
job_result.start()
|
||||
job.start()
|
||||
|
||||
module = ReportModule.objects.get(pk=job_result.object_id)
|
||||
report = module.reports.get(job_result.name)()
|
||||
module = ReportModule.objects.get(pk=job.object_id)
|
||||
report = module.reports.get(job.name)()
|
||||
|
||||
try:
|
||||
report.run(job_result)
|
||||
report.run(job)
|
||||
except Exception:
|
||||
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
logging.error(f"Error during execution of report {job_result.name}")
|
||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
logging.error(f"Error during execution of report {job.name}")
|
||||
finally:
|
||||
# Schedule the next job if an interval has been set
|
||||
if job_result.interval:
|
||||
new_scheduled_time = job_result.scheduled + timedelta(minutes=job_result.interval)
|
||||
if job.interval:
|
||||
new_scheduled_time = job.scheduled + timedelta(minutes=job.interval)
|
||||
Job.enqueue(
|
||||
run_report,
|
||||
instance=job_result.object,
|
||||
name=job_result.name,
|
||||
user=job_result.user,
|
||||
instance=job.object,
|
||||
name=job.name,
|
||||
user=job.user,
|
||||
job_timeout=report.job_timeout,
|
||||
schedule_at=new_scheduled_time,
|
||||
interval=job_result.interval
|
||||
interval=job.interval
|
||||
)
|
||||
|
||||
|
||||
@ -190,13 +190,13 @@ class Report(object):
|
||||
# Run methods
|
||||
#
|
||||
|
||||
def run(self, job_result):
|
||||
def run(self, job):
|
||||
"""
|
||||
Run the report and save its results. Each test method will be executed in order.
|
||||
"""
|
||||
self.logger.info(f"Running report")
|
||||
job_result.status = JobStatusChoices.STATUS_RUNNING
|
||||
job_result.save()
|
||||
job.status = JobStatusChoices.STATUS_RUNNING
|
||||
job.save()
|
||||
|
||||
# Perform any post-run tasks
|
||||
self.pre_run()
|
||||
@ -208,17 +208,17 @@ class Report(object):
|
||||
test_method()
|
||||
if self.failed:
|
||||
self.logger.warning("Report failed")
|
||||
job_result.status = JobStatusChoices.STATUS_FAILED
|
||||
job.status = JobStatusChoices.STATUS_FAILED
|
||||
else:
|
||||
self.logger.info("Report completed successfully")
|
||||
job_result.status = JobStatusChoices.STATUS_COMPLETED
|
||||
job.status = JobStatusChoices.STATUS_COMPLETED
|
||||
except Exception as e:
|
||||
stacktrace = traceback.format_exc()
|
||||
self.log_failure(None, f"An exception occurred: {type(e).__name__}: {e} <pre>{stacktrace}</pre>")
|
||||
logger.error(f"Exception raised during report execution: {e}")
|
||||
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
finally:
|
||||
job_result.terminate()
|
||||
job.terminate()
|
||||
|
||||
# Perform any post-run tasks
|
||||
self.post_run()
|
||||
|
@ -444,16 +444,15 @@ def get_module_and_script(module_name, script_name):
|
||||
return module, script
|
||||
|
||||
|
||||
def run_script(data, request, commit=True, *args, **kwargs):
|
||||
def run_script(data, request, job, commit=True, **kwargs):
|
||||
"""
|
||||
A wrapper for calling Script.run(). This performs error handling and provides a hook for committing changes. It
|
||||
exists outside the Script class to ensure it cannot be overridden by a script author.
|
||||
"""
|
||||
job_result = kwargs.pop('job_result')
|
||||
job_result.start()
|
||||
job.start()
|
||||
|
||||
module = ScriptModule.objects.get(pk=job_result.object_id)
|
||||
script = module.scripts.get(job_result.name)()
|
||||
module = ScriptModule.objects.get(pk=job.object_id)
|
||||
script = module.scripts.get(job.name)()
|
||||
|
||||
logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
|
||||
logger.info(f"Running script (commit={commit})")
|
||||
@ -480,8 +479,8 @@ def run_script(data, request, commit=True, *args, **kwargs):
|
||||
except AbortTransaction:
|
||||
script.log_info("Database changes have been reverted automatically.")
|
||||
clear_webhooks.send(request)
|
||||
job_result.data = ScriptOutputSerializer(script).data
|
||||
job_result.terminate()
|
||||
job.data = ScriptOutputSerializer(script).data
|
||||
job.terminate()
|
||||
except Exception as e:
|
||||
if type(e) is AbortScript:
|
||||
script.log_failure(f"Script aborted with error: {e}")
|
||||
@ -491,11 +490,11 @@ def run_script(data, request, commit=True, *args, **kwargs):
|
||||
script.log_failure(f"An exception occurred: `{type(e).__name__}: {e}`\n```\n{stacktrace}\n```")
|
||||
logger.error(f"Exception raised during script execution: {e}")
|
||||
script.log_info("Database changes have been reverted due to error.")
|
||||
job_result.data = ScriptOutputSerializer(script).data
|
||||
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
job.data = ScriptOutputSerializer(script).data
|
||||
job.terminate(status=JobStatusChoices.STATUS_ERRORED)
|
||||
clear_webhooks.send(request)
|
||||
|
||||
logger.info(f"Script completed in {job_result.duration}")
|
||||
logger.info(f"Script completed in {job.duration}")
|
||||
|
||||
# Execute the script. If commit is True, wrap it with the change_logging context manager to ensure we process
|
||||
# change logging, webhooks, etc.
|
||||
@ -506,15 +505,15 @@ def run_script(data, request, commit=True, *args, **kwargs):
|
||||
_run_script()
|
||||
|
||||
# Schedule the next job if an interval has been set
|
||||
if job_result.interval:
|
||||
new_scheduled_time = job_result.scheduled + timedelta(minutes=job_result.interval)
|
||||
if job.interval:
|
||||
new_scheduled_time = job.scheduled + timedelta(minutes=job.interval)
|
||||
Job.enqueue(
|
||||
run_script,
|
||||
instance=job_result.object,
|
||||
name=job_result.name,
|
||||
user=job_result.user,
|
||||
instance=job.object,
|
||||
name=job.name,
|
||||
user=job.user,
|
||||
schedule_at=new_scheduled_time,
|
||||
interval=job_result.interval,
|
||||
interval=job.interval,
|
||||
job_timeout=script.job_timeout,
|
||||
data=data,
|
||||
request=request,
|
||||
|
@ -820,7 +820,7 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
|
||||
report_modules = ReportModule.objects.restrict(request.user)
|
||||
|
||||
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
||||
job_results = {
|
||||
jobs = {
|
||||
r.name: r
|
||||
for r in Job.objects.filter(
|
||||
object_type=report_content_type,
|
||||
@ -831,7 +831,7 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
|
||||
return render(request, 'extras/report_list.html', {
|
||||
'model': ReportModule,
|
||||
'report_modules': report_modules,
|
||||
'job_results': job_results,
|
||||
'jobs': jobs,
|
||||
})
|
||||
|
||||
|
||||
@ -988,7 +988,7 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
|
||||
script_modules = ScriptModule.objects.restrict(request.user)
|
||||
|
||||
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
||||
job_results = {
|
||||
jobs = {
|
||||
r.name: r
|
||||
for r in Job.objects.filter(
|
||||
object_type=script_content_type,
|
||||
@ -999,7 +999,7 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
|
||||
return render(request, 'extras/script_list.html', {
|
||||
'model': ScriptModule,
|
||||
'script_modules': script_modules,
|
||||
'job_results': job_results,
|
||||
'jobs': jobs,
|
||||
})
|
||||
|
||||
|
||||
|
@ -50,7 +50,7 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for report_name, report in module.reports.items %}
|
||||
{% with last_result=job_results|get_key:report.full_name %}
|
||||
{% with last_result=jobs|get_key:report.full_name %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{% url 'extras:report' module=module.path name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a>
|
||||
|
@ -48,7 +48,7 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for script_name, script_class in module.scripts.items %}
|
||||
{% with last_result=job_results|get_key:script_class.full_name %}
|
||||
{% with last_result=jobs|get_key:script_class.full_name %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{% url 'extras:script' module=module.path name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
|
||||
|
Loading…
Reference in New Issue
Block a user