mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
Allow setting individual timeouts on scripts and reports
This commit is contained in:
parent
e2b6d69596
commit
36d6ae33d1
@ -89,6 +89,10 @@ The checkbox to commit database changes when executing a script is checked by de
|
|||||||
commit_default = False
|
commit_default = False
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `job_timeout`
|
||||||
|
|
||||||
|
Set the allowed runtime of a script. If not set the `RQ_DEFAULT_TIMEOUT` will be used.
|
||||||
|
|
||||||
## Accessing Request Data
|
## Accessing Request Data
|
||||||
|
|
||||||
Details of the current HTTP request (the one being made to execute the script) are available as the instance attribute `self.request`. This can be used to infer, for example, the user executing the script and the client IP address:
|
Details of the current HTTP request (the one being made to execute the script) are available as the instance attribute `self.request`. This can be used to infer, for example, the user executing the script and the client IP address:
|
||||||
|
@ -236,7 +236,8 @@ class ReportViewSet(ViewSet):
|
|||||||
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
|
||||||
)
|
)
|
||||||
report.result = job_result
|
report.result = job_result
|
||||||
|
|
||||||
@ -320,7 +321,8 @@ class ScriptViewSet(ViewSet):
|
|||||||
request.user,
|
request.user,
|
||||||
data=data,
|
data=data,
|
||||||
request=copy_safe_request(request),
|
request=copy_safe_request(request),
|
||||||
commit=commit
|
commit=commit,
|
||||||
|
job_timeout=script.job_timeout,
|
||||||
)
|
)
|
||||||
script.result = job_result
|
script.result = job_result
|
||||||
serializer = serializers.ScriptDetailSerializer(script, context={'request': request})
|
serializer = serializers.ScriptDetailSerializer(script, context={'request': request})
|
||||||
|
@ -35,7 +35,8 @@ class Command(BaseCommand):
|
|||||||
run_report,
|
run_report,
|
||||||
report.full_name,
|
report.full_name,
|
||||||
report_content_type,
|
report_content_type,
|
||||||
None
|
None,
|
||||||
|
job_timeout=report.job_timeout
|
||||||
)
|
)
|
||||||
|
|
||||||
# Wait on the job to finish
|
# Wait on the job to finish
|
||||||
|
@ -13,6 +13,7 @@ from django.urls import reverse
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.formats import date_format
|
from django.utils.formats import date_format
|
||||||
from rest_framework.utils.encoders import JSONEncoder
|
from rest_framework.utils.encoders import JSONEncoder
|
||||||
|
import django_rq
|
||||||
|
|
||||||
from extras.choices import *
|
from extras.choices import *
|
||||||
from extras.constants import *
|
from extras.constants import *
|
||||||
@ -550,7 +551,8 @@ class JobResult(models.Model):
|
|||||||
job_id=uuid.uuid4()
|
job_id=uuid.uuid4()
|
||||||
)
|
)
|
||||||
|
|
||||||
func.delay(*args, job_id=str(job_result.job_id), job_result=job_result, **kwargs)
|
queue = django_rq.get_queue("default")
|
||||||
|
queue.enqueue(func, job_id=str(job_result.job_id), job_result=job_result, **kwargs)
|
||||||
|
|
||||||
return job_result
|
return job_result
|
||||||
|
|
||||||
|
@ -119,6 +119,7 @@ class Report(object):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
description = None
|
description = None
|
||||||
|
job_timeout = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
|
@ -298,6 +298,10 @@ class BaseScript:
|
|||||||
def module(cls):
|
def module(cls):
|
||||||
return cls.__module__
|
return cls.__module__
|
||||||
|
|
||||||
|
@classproperty
|
||||||
|
def job_timeout(self):
|
||||||
|
return getattr(self.Meta, 'job_timeout', None)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_vars(cls):
|
def _get_vars(cls):
|
||||||
vars = {}
|
vars = {}
|
||||||
@ -414,7 +418,7 @@ def is_variable(obj):
|
|||||||
return isinstance(obj, ScriptVariable)
|
return isinstance(obj, ScriptVariable)
|
||||||
|
|
||||||
|
|
||||||
@job('default')
|
#@job('default')
|
||||||
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
|
||||||
|
@ -588,7 +588,8 @@ class ReportView(ContentTypePermissionRequiredMixin, View):
|
|||||||
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
|
||||||
)
|
)
|
||||||
|
|
||||||
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
||||||
@ -708,6 +709,7 @@ class ScriptView(ContentTypePermissionRequiredMixin, GetScriptMixin, View):
|
|||||||
commit = form.cleaned_data.pop('_commit')
|
commit = form.cleaned_data.pop('_commit')
|
||||||
|
|
||||||
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
script_content_type = ContentType.objects.get(app_label='extras', model='script')
|
||||||
|
|
||||||
job_result = JobResult.enqueue_job(
|
job_result = JobResult.enqueue_job(
|
||||||
run_script,
|
run_script,
|
||||||
script.full_name,
|
script.full_name,
|
||||||
@ -715,7 +717,8 @@ class ScriptView(ContentTypePermissionRequiredMixin, GetScriptMixin, View):
|
|||||||
request.user,
|
request.user,
|
||||||
data=form.cleaned_data,
|
data=form.cleaned_data,
|
||||||
request=copy_safe_request(request),
|
request=copy_safe_request(request),
|
||||||
commit=commit
|
commit=commit,
|
||||||
|
job_timeout=script.job_timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
return redirect('extras:script_result', job_result_pk=job_result.pk)
|
return redirect('extras:script_result', job_result_pk=job_result.pk)
|
||||||
|
Loading…
Reference in New Issue
Block a user