Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/customization/custom-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ The checkbox to commit database changes when executing a script is checked by de
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

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:
Expand Down
6 changes: 4 additions & 2 deletions netbox/extras/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ def run(self, request, pk):
run_report,
report.full_name,
report_content_type,
request.user
request.user,
job_timeout=report.job_timeout
)
report.result = job_result

Expand Down Expand Up @@ -320,7 +321,8 @@ def post(self, request, pk):
request.user,
data=data,
request=copy_safe_request(request),
commit=commit
commit=commit,
job_timeout=script.job_timeout,
)
script.result = job_result
serializer = serializers.ScriptDetailSerializer(script, context={'request': request})
Expand Down
3 changes: 2 additions & 1 deletion netbox/extras/management/commands/runreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def handle(self, *args, **options):
run_report,
report.full_name,
report_content_type,
None
None,
job_timeout=report.job_timeout
)

# Wait on the job to finish
Expand Down
4 changes: 3 additions & 1 deletion netbox/extras/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils import timezone
from django.utils.formats import date_format
from rest_framework.utils.encoders import JSONEncoder
import django_rq

from extras.choices import *
from extras.constants import *
Expand Down Expand Up @@ -550,7 +551,8 @@ def enqueue_job(cls, func, name, obj_type, user, *args, **kwargs):
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know enough about django_rq to know if there is a reason to use the job decorator over just enqueuing the job.

I do like that doing this also gives us the ability to schedule jobs down the line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have the same net effect either way.


return job_result

Expand Down
1 change: 1 addition & 0 deletions netbox/extras/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Report(object):
}
"""
description = None
job_timeout = None

def __init__(self):

Expand Down
5 changes: 4 additions & 1 deletion netbox/extras/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ def description(self):
def module(cls):
return cls.__module__

@classproperty
def job_timeout(self):
return getattr(self.Meta, 'job_timeout', None)

@classmethod
def _get_vars(cls):
vars = {}
Expand Down Expand Up @@ -414,7 +418,6 @@ def is_variable(obj):
return isinstance(obj, ScriptVariable)


@job('default')
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
Expand Down
7 changes: 5 additions & 2 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ def post(self, request, module, name):
run_report,
report.full_name,
report_content_type,
request.user
request.user,
job_timeout=report.job_timeout
)

return redirect('extras:report_result', job_result_pk=job_result.pk)
Expand Down Expand Up @@ -708,14 +709,16 @@ def post(self, request, module, name):
commit = form.cleaned_data.pop('_commit')

script_content_type = ContentType.objects.get(app_label='extras', model='script')

job_result = JobResult.enqueue_job(
run_script,
script.full_name,
script_content_type,
request.user,
data=form.cleaned_data,
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)
Expand Down