Clean up old naming

This commit is contained in:
jeremystretch 2023-03-28 15:24:43 -04:00
parent 4449003b59
commit c10793176f
10 changed files with 62 additions and 65 deletions

View File

@ -8,22 +8,22 @@ from .models import DataSource
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def sync_datasource(job_result, *args, **kwargs): def sync_datasource(job, *args, **kwargs):
""" """
Call sync() on a DataSource. Call sync() on a DataSource.
""" """
datasource = DataSource.objects.get(pk=job_result.object_id) datasource = DataSource.objects.get(pk=job.object_id)
try: try:
job_result.start() job.start()
datasource.sync() datasource.sync()
# Update the search cache for DataFiles belonging to this source # Update the search cache for DataFiles belonging to this source
search_backend.cache(datasource.datafiles.iterator()) search_backend.cache(datasource.datafiles.iterator())
job_result.terminate() job.terminate()
except SyncError as e: 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) DataSource.objects.filter(pk=datasource.pk).update(status=DataSourceStatusChoices.FAILED)
logging.error(e) logging.error(e)

View File

@ -119,14 +119,12 @@ class DataSource(JobsMixin, PrimaryModel):
DataSource.objects.filter(pk=self.pk).update(status=self.status) DataSource.objects.filter(pk=self.pk).update(status=self.status)
# Enqueue a sync job # Enqueue a sync job
job_result = Job.enqueue( return Job.enqueue(
import_string('core.jobs.sync_datasource'), import_string('core.jobs.sync_datasource'),
instance=self, instance=self,
user=request.user user=request.user
) )
return job_result
def get_backend(self): def get_backend(self):
backend_cls = registry['data_backends'].get(self.type) backend_cls = registry['data_backends'].get(self.type)
backend_params = self.parameters or {} backend_params = self.parameters or {}

View File

@ -190,9 +190,9 @@ class Job(models.Model):
) )
if schedule_at: 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: 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 return job

View File

@ -55,9 +55,9 @@ class DataSourceSyncView(BaseObjectView):
def post(self, request, pk): def post(self, request, pk):
datasource = get_object_or_404(self.queryset, pk=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()) return redirect(datasource.get_absolute_url())

View File

@ -48,8 +48,8 @@ class Command(BaseCommand):
except AbortTransaction: except AbortTransaction:
script.log_info("Database changes have been reverted automatically.") script.log_info("Database changes have been reverted automatically.")
clear_webhooks.send(request) clear_webhooks.send(request)
job_result.data = ScriptOutputSerializer(script).data job.data = ScriptOutputSerializer(script).data
job_result.terminate() job.terminate()
except Exception as e: except Exception as e:
stacktrace = traceback.format_exc() stacktrace = traceback.format_exc()
script.log_failure( script.log_failure(
@ -58,10 +58,10 @@ class Command(BaseCommand):
script.log_info("Database changes have been reverted due to error.") script.log_info("Database changes have been reverted due to error.")
logger.error(f"Exception raised during script execution: {e}") logger.error(f"Exception raised during script execution: {e}")
clear_webhooks.send(request) clear_webhooks.send(request)
job_result.data = ScriptOutputSerializer(script).data job.data = ScriptOutputSerializer(script).data
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED) job.terminate(status=JobStatusChoices.STATUS_ERRORED)
logger.info(f"Script completed in {job_result.duration}") logger.info(f"Script completed in {job.duration}")
# Params # Params
script = options['script'] script = options['script']
@ -110,7 +110,7 @@ class Command(BaseCommand):
form = script.as_form(data, None) form = script.as_form(data, None)
# Create the job # Create the job
job_result = Job.objects.create( job = Job.objects.create(
instance=module, instance=module,
name=script.name, 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],
@ -124,12 +124,12 @@ class Command(BaseCommand):
'FILES': {}, 'FILES': {},
'user': user, 'user': user,
'path': '', 'path': '',
'id': job_result.job_id 'id': job.job_id
}) })
if form.is_valid(): if form.is_valid():
job_result.status = JobStatusChoices.STATUS_RUNNING job.status = JobStatusChoices.STATUS_RUNNING
job_result.save() job.save()
logger.info(f"Running script (commit={commit})") logger.info(f"Running script (commit={commit})")
script.request = request script.request = request
@ -143,5 +143,5 @@ class Command(BaseCommand):
for field, errors in form.errors.get_json_data().items(): for field, errors in form.errors.get_json_data().items():
for error in errors: for error in errors:
logger.error(f'\t{field}: {error.get("message")}') logger.error(f'\t{field}: {error.get("message")}')
job_result.status = JobStatusChoices.STATUS_ERRORED job.status = JobStatusChoices.STATUS_ERRORED
job_result.save() job.save()

View File

@ -27,33 +27,33 @@ def get_module_and_report(module_name, report_name):
@job('default') @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 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. method for queueing into the background processor.
""" """
job_result.start() job.start()
module = ReportModule.objects.get(pk=job_result.object_id) module = ReportModule.objects.get(pk=job.object_id)
report = module.reports.get(job_result.name)() report = module.reports.get(job.name)()
try: try:
report.run(job_result) report.run(job)
except Exception: except Exception:
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED) job.terminate(status=JobStatusChoices.STATUS_ERRORED)
logging.error(f"Error during execution of report {job_result.name}") logging.error(f"Error during execution of report {job.name}")
finally: finally:
# Schedule the next job if an interval has been set # Schedule the next job if an interval has been set
if job_result.interval: if job.interval:
new_scheduled_time = job_result.scheduled + timedelta(minutes=job_result.interval) new_scheduled_time = job.scheduled + timedelta(minutes=job.interval)
Job.enqueue( Job.enqueue(
run_report, run_report,
instance=job_result.object, instance=job.object,
name=job_result.name, name=job.name,
user=job_result.user, user=job.user,
job_timeout=report.job_timeout, job_timeout=report.job_timeout,
schedule_at=new_scheduled_time, schedule_at=new_scheduled_time,
interval=job_result.interval interval=job.interval
) )
@ -190,13 +190,13 @@ class Report(object):
# Run methods # 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. Run the report and save its results. Each test method will be executed in order.
""" """
self.logger.info(f"Running report") self.logger.info(f"Running report")
job_result.status = JobStatusChoices.STATUS_RUNNING job.status = JobStatusChoices.STATUS_RUNNING
job_result.save() job.save()
# Perform any post-run tasks # Perform any post-run tasks
self.pre_run() self.pre_run()
@ -208,17 +208,17 @@ class Report(object):
test_method() test_method()
if self.failed: if self.failed:
self.logger.warning("Report failed") self.logger.warning("Report failed")
job_result.status = JobStatusChoices.STATUS_FAILED job.status = JobStatusChoices.STATUS_FAILED
else: else:
self.logger.info("Report completed successfully") self.logger.info("Report completed successfully")
job_result.status = JobStatusChoices.STATUS_COMPLETED job.status = JobStatusChoices.STATUS_COMPLETED
except Exception as e: except Exception as e:
stacktrace = traceback.format_exc() stacktrace = traceback.format_exc()
self.log_failure(None, f"An exception occurred: {type(e).__name__}: {e} <pre>{stacktrace}</pre>") self.log_failure(None, f"An exception occurred: {type(e).__name__}: {e} <pre>{stacktrace}</pre>")
logger.error(f"Exception raised during report execution: {e}") logger.error(f"Exception raised during report execution: {e}")
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED) job.terminate(status=JobStatusChoices.STATUS_ERRORED)
finally: finally:
job_result.terminate() job.terminate()
# Perform any post-run tasks # Perform any post-run tasks
self.post_run() self.post_run()

View File

@ -444,16 +444,15 @@ def get_module_and_script(module_name, script_name):
return module, script 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 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. exists outside the Script class to ensure it cannot be overridden by a script author.
""" """
job_result = kwargs.pop('job_result') job.start()
job_result.start()
module = ScriptModule.objects.get(pk=job_result.object_id) module = ScriptModule.objects.get(pk=job.object_id)
script = module.scripts.get(job_result.name)() script = module.scripts.get(job.name)()
logger = logging.getLogger(f"netbox.scripts.{script.full_name}") logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
logger.info(f"Running script (commit={commit})") logger.info(f"Running script (commit={commit})")
@ -480,8 +479,8 @@ def run_script(data, request, commit=True, *args, **kwargs):
except AbortTransaction: except AbortTransaction:
script.log_info("Database changes have been reverted automatically.") script.log_info("Database changes have been reverted automatically.")
clear_webhooks.send(request) clear_webhooks.send(request)
job_result.data = ScriptOutputSerializer(script).data job.data = ScriptOutputSerializer(script).data
job_result.terminate() job.terminate()
except Exception as e: except Exception as e:
if type(e) is AbortScript: if type(e) is AbortScript:
script.log_failure(f"Script aborted with error: {e}") 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```") script.log_failure(f"An exception occurred: `{type(e).__name__}: {e}`\n```\n{stacktrace}\n```")
logger.error(f"Exception raised during script execution: {e}") logger.error(f"Exception raised during script execution: {e}")
script.log_info("Database changes have been reverted due to error.") script.log_info("Database changes have been reverted due to error.")
job_result.data = ScriptOutputSerializer(script).data job.data = ScriptOutputSerializer(script).data
job_result.terminate(status=JobStatusChoices.STATUS_ERRORED) job.terminate(status=JobStatusChoices.STATUS_ERRORED)
clear_webhooks.send(request) 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 # Execute the script. If commit is True, wrap it with the change_logging context manager to ensure we process
# change logging, webhooks, etc. # change logging, webhooks, etc.
@ -506,15 +505,15 @@ def run_script(data, request, commit=True, *args, **kwargs):
_run_script() _run_script()
# Schedule the next job if an interval has been set # Schedule the next job if an interval has been set
if job_result.interval: if job.interval:
new_scheduled_time = job_result.scheduled + timedelta(minutes=job_result.interval) new_scheduled_time = job.scheduled + timedelta(minutes=job.interval)
Job.enqueue( Job.enqueue(
run_script, run_script,
instance=job_result.object, instance=job.object,
name=job_result.name, name=job.name,
user=job_result.user, user=job.user,
schedule_at=new_scheduled_time, schedule_at=new_scheduled_time,
interval=job_result.interval, interval=job.interval,
job_timeout=script.job_timeout, job_timeout=script.job_timeout,
data=data, data=data,
request=request, request=request,

View File

@ -820,7 +820,7 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
report_modules = ReportModule.objects.restrict(request.user) report_modules = ReportModule.objects.restrict(request.user)
report_content_type = ContentType.objects.get(app_label='extras', model='report') report_content_type = ContentType.objects.get(app_label='extras', model='report')
job_results = { jobs = {
r.name: r r.name: r
for r in Job.objects.filter( for r in Job.objects.filter(
object_type=report_content_type, object_type=report_content_type,
@ -831,7 +831,7 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
return render(request, 'extras/report_list.html', { return render(request, 'extras/report_list.html', {
'model': ReportModule, 'model': ReportModule,
'report_modules': report_modules, '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_modules = ScriptModule.objects.restrict(request.user)
script_content_type = ContentType.objects.get(app_label='extras', model='script') script_content_type = ContentType.objects.get(app_label='extras', model='script')
job_results = { jobs = {
r.name: r r.name: r
for r in Job.objects.filter( for r in Job.objects.filter(
object_type=script_content_type, object_type=script_content_type,
@ -999,7 +999,7 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
return render(request, 'extras/script_list.html', { return render(request, 'extras/script_list.html', {
'model': ScriptModule, 'model': ScriptModule,
'script_modules': script_modules, 'script_modules': script_modules,
'job_results': job_results, 'jobs': jobs,
}) })

View File

@ -50,7 +50,7 @@
</thead> </thead>
<tbody> <tbody>
{% for report_name, report in module.reports.items %} {% 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> <tr>
<td> <td>
<a href="{% url 'extras:report' module=module.path name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a> <a href="{% url 'extras:report' module=module.path name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a>

View File

@ -48,7 +48,7 @@
</thead> </thead>
<tbody> <tbody>
{% for script_name, script_class in module.scripts.items %} {% 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> <tr>
<td> <td>
<a href="{% url 'extras:script' module=module.path name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a> <a href="{% url 'extras:script' module=module.path name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>