mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 17:08:41 -06:00
Job scheduling review changes
This commit is contained in:
parent
893925436d
commit
ed2f7f1236
@ -81,7 +81,7 @@ class Command(BaseCommand):
|
|||||||
ending=""
|
ending=""
|
||||||
)
|
)
|
||||||
self.stdout.flush()
|
self.stdout.flush()
|
||||||
JobResult.objects.filter(created__lt=cutoff).delete(using=DEFAULT_DB_ALIAS)
|
JobResult.objects.filter(created__lt=cutoff).delete()
|
||||||
if options['verbosity']:
|
if options['verbosity']:
|
||||||
self.stdout.write("Done.", self.style.SUCCESS)
|
self.stdout.write("Done.", self.style.SUCCESS)
|
||||||
elif options['verbosity']:
|
elif options['verbosity']:
|
||||||
|
@ -569,7 +569,7 @@ class JobResult(models.Model):
|
|||||||
self.completed = timezone.now()
|
self.completed = timezone.now()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def enqueue_job(cls, func, name, obj_type, user, *args, **kwargs):
|
def enqueue_job(cls, func, name, obj_type, user, schedule_at=None, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Create a JobResult instance and enqueue a job using the given callable
|
Create a JobResult instance and enqueue a job using the given callable
|
||||||
|
|
||||||
@ -577,6 +577,7 @@ class JobResult(models.Model):
|
|||||||
name: Name for the JobResult instance
|
name: Name for the JobResult instance
|
||||||
obj_type: ContentType to link to the JobResult instance obj_type
|
obj_type: ContentType to link to the JobResult instance obj_type
|
||||||
user: User object to link to the JobResult instance
|
user: User object to link to the JobResult instance
|
||||||
|
schedule_at: Schedule the job to be executed at the passed date and time
|
||||||
args: additional args passed to the callable
|
args: additional args passed to the callable
|
||||||
kwargs: additional kargs passed to the callable
|
kwargs: additional kargs passed to the callable
|
||||||
"""
|
"""
|
||||||
@ -589,7 +590,7 @@ class JobResult(models.Model):
|
|||||||
|
|
||||||
queue = django_rq.get_queue("default")
|
queue = django_rq.get_queue("default")
|
||||||
|
|
||||||
if schedule_at := kwargs.pop("schedule_at", None):
|
if schedule_at:
|
||||||
job_result.status = JobResultStatusChoices.STATUS_SCHEDULED
|
job_result.status = JobResultStatusChoices.STATUS_SCHEDULED
|
||||||
job_result.scheduled_time = schedule_at
|
job_result.scheduled_time = schedule_at
|
||||||
job_result.save()
|
job_result.save()
|
||||||
|
@ -612,26 +612,30 @@ class ReportView(ContentTypePermissionRequiredMixin, View):
|
|||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
schedule_at = form.cleaned_data.get("schedule_at")
|
schedule_at = form.cleaned_data.get("schedule_at")
|
||||||
|
|
||||||
# Allow execution only if RQ worker process is running
|
# Allow execution only if RQ worker process is running
|
||||||
if not Worker.count(get_connection('default')):
|
if not Worker.count(get_connection('default')):
|
||||||
messages.error(request, "Unable to run report: RQ worker process not running.")
|
messages.error(request, "Unable to run report: RQ worker process not running.")
|
||||||
return render(request, 'extras/report.html', {
|
return render(request, 'extras/report.html', {
|
||||||
'report': report,
|
'report': report,
|
||||||
})
|
})
|
||||||
|
|
||||||
# Run the Report. A new JobResult is created.
|
# Run the Report. A new JobResult is created.
|
||||||
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
||||||
job_result = JobResult.enqueue_job(
|
job_result = JobResult.enqueue_job(
|
||||||
run_report,
|
run_report,
|
||||||
report.full_name,
|
report.full_name,
|
||||||
report_content_type,
|
report_content_type,
|
||||||
request.user,
|
request.user,
|
||||||
job_timeout=report.job_timeout,
|
job_timeout=report.job_timeout,
|
||||||
schedule_at=schedule_at,
|
schedule_at=schedule_at,
|
||||||
)
|
)
|
||||||
|
|
||||||
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
|
||||||
|
|
||||||
|
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
||||||
|
|
||||||
|
return render(request, 'extras/report.html', {
|
||||||
|
'report': report,
|
||||||
|
'form': form,
|
||||||
|
})
|
||||||
|
|
||||||
class ReportResultView(ContentTypePermissionRequiredMixin, View):
|
class ReportResultView(ContentTypePermissionRequiredMixin, View):
|
||||||
"""
|
"""
|
||||||
@ -804,10 +808,6 @@ class ScriptResultView(ContentTypePermissionRequiredMixin, GetScriptMixin, View)
|
|||||||
# Job results
|
# Job results
|
||||||
#
|
#
|
||||||
|
|
||||||
class JobResultView(generic.ObjectView):
|
|
||||||
queryset = JobResult.objects.all()
|
|
||||||
|
|
||||||
|
|
||||||
class JobResultListView(generic.ObjectListView):
|
class JobResultListView(generic.ObjectListView):
|
||||||
queryset = JobResult.objects.all()
|
queryset = JobResult.objects.all()
|
||||||
filterset = filtersets.JobResultFilterSet
|
filterset = filtersets.JobResultFilterSet
|
||||||
|
Loading…
Reference in New Issue
Block a user