Custom script scheduling no more possible #7554

Closed
opened 2025-12-29 20:25:10 +01:00 by adam · 8 comments
Owner

Originally created by @ChMG on GitHub (Jan 24, 2023).

Originally assigned to: @abhi1693 on GitHub.

NetBox version

v3.4.3

Python version

3.10

Steps to Reproduce

  1. go to "Other" -> "Scripts"
  2. select the script to schedule
  3. fill the fields "Schedule at" and "Recurs every" to the correct values.
  4. click "Run Script"

Expected Behavior

An sheduled task to run the script is created for the configured time.

Observed Behavior

An exception was raised: NameError: name 'timezone' is not defined

2023-01-24 16:19:03,640 django.request ERROR: Internal Server Error: /extras/scripts/ipscan.IPScan/ Traceback (most recent call last): File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py", line 53, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch return handler(request, *args, **kwargs) File "/opt/netbox/netbox/extras/views.py", line 815, in post elif form.is_valid(): File "/opt/netbox/netbox/utilities/forms/forms.py", line 69, in is_valid is_valid = super().is_valid() File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid return self.is_bound and not self.errors File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors self.full_clean() File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 437, in full_clean self._clean_fields() File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 452, in _clean_fields value = getattr(self, "clean_%s" % name)() File "/opt/netbox/netbox/extras/forms/scripts.py", line 50, in clean__schedule_at if scheduled_time and scheduled_time < timezone.now(): NameError: name 'timezone' is not defined

Originally created by @ChMG on GitHub (Jan 24, 2023). Originally assigned to: @abhi1693 on GitHub. ### NetBox version v3.4.3 ### Python version 3.10 ### Steps to Reproduce 1. go to "Other" -> "Scripts" 2. select the script to schedule 3. fill the fields "Schedule at" and "Recurs every" to the correct values. 4. click "Run Script" ### Expected Behavior An sheduled task to run the script is created for the configured time. ### Observed Behavior An exception was raised: NameError: name 'timezone' is not defined ` 2023-01-24 16:19:03,640 django.request ERROR: Internal Server Error: /extras/scripts/ipscan.IPScan/ Traceback (most recent call last): File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py", line 53, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch return handler(request, *args, **kwargs) File "/opt/netbox/netbox/extras/views.py", line 815, in post elif form.is_valid(): File "/opt/netbox/netbox/utilities/forms/forms.py", line 69, in is_valid is_valid = super().is_valid() File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid return self.is_bound and not self.errors File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors self.full_clean() File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 437, in full_clean self._clean_fields() File "/opt/netbox-3.4.3/venv/lib/python3.10/site-packages/django/forms/forms.py", line 452, in _clean_fields value = getattr(self, "clean_%s" % name)() File "/opt/netbox/netbox/extras/forms/scripts.py", line 50, in clean__schedule_at if scheduled_time and scheduled_time < timezone.now(): NameError: name 'timezone' is not defined `
adam added the type: bugstatus: accepted labels 2025-12-29 20:25:10 +01:00
adam closed this issue 2025-12-29 20:25:10 +01:00
Author
Owner

@ChMG commented on GitHub (Jan 24, 2023):

I have solved the problem at my installation.

Edit /opt/netbox/netbox/extras/forms/scripts.py line 50.

From:
if scheduled_time and scheduled_time < timezone.now():

To:
if scheduled_time and scheduled_time < local_now():

@ChMG commented on GitHub (Jan 24, 2023): I have solved the problem at my installation. Edit /opt/netbox/netbox/extras/forms/scripts.py line 50. From: ` if scheduled_time and scheduled_time < timezone.now():` To: `if scheduled_time and scheduled_time < local_now():`
Author
Owner

@abhi1693 commented on GitHub (Jan 24, 2023):

I have a similar problem, in my case I only enter Recurs every and get a different error

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/rq/worker.py", line 1111, in perform_job
    rv = job.perform()
  File "/usr/local/lib/python3.10/site-packages/rq/job.py", line 923, in perform
    self._result = self._execute()
  File "/usr/local/lib/python3.10/site-packages/rq/job.py", line 946, in _execute
    result = self.func(*self.args, **self.kwargs)
  File "/app/netbox/extras/scripts.py", line 498, in run_script
    new_scheduled_time = job_result.scheduled + timedelta(minutes=job_result.interval)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta
@abhi1693 commented on GitHub (Jan 24, 2023): I have a similar problem, in my case I only enter `Recurs every` and get a different error ``` Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/rq/worker.py", line 1111, in perform_job rv = job.perform() File "/usr/local/lib/python3.10/site-packages/rq/job.py", line 923, in perform self._result = self._execute() File "/usr/local/lib/python3.10/site-packages/rq/job.py", line 946, in _execute result = self.func(*self.args, **self.kwargs) File "/app/netbox/extras/scripts.py", line 498, in run_script new_scheduled_time = job_result.scheduled + timedelta(minutes=job_result.interval) TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta ```
Author
Owner

@ChMG commented on GitHub (Jan 24, 2023):

At my first try with scheduling after this function has been released, I get the same error.
After I look at the code I see, both fields "Schedule at" and "Recurs every" are required.

So you have to enter an date and time for the first scheduled run at "Schedule at".

@ChMG commented on GitHub (Jan 24, 2023): At my first try with scheduling after this function has been released, I get the same error. After I look at the code I see, both fields "Schedule at" and "Recurs every" are required. So you have to enter an date and time for the first scheduled run at "Schedule at".
Author
Owner

@abhi1693 commented on GitHub (Jan 24, 2023):

Well, it shows the current datetime and as a user I thought it would take that as the scheduling parameter

image

@abhi1693 commented on GitHub (Jan 24, 2023): Well, it shows the current datetime and as a user I thought it would take that as the scheduling parameter ![image](https://user-images.githubusercontent.com/5083532/214397935-80f27814-7f64-4277-8186-752c936ba2c1.png)
Author
Owner

@abhi1693 commented on GitHub (Jan 24, 2023):

I see this being fixed 2 ways,

  • Assume that when a user enters recurs every only, use the current datetime in the schedule at automatically. This is what I'll prefer.
  • Raise an error in the form that both are required when only recurs every is used

I have patches for both along with the timezone issue. Whatever is accepted, I can submit a PR

@abhi1693 commented on GitHub (Jan 24, 2023): I see this being fixed 2 ways, - Assume that when a user enters recurs every only, use the current datetime in the schedule at automatically. This is what I'll prefer. - Raise an error in the form that both are required when only recurs every is used I have patches for both along with the timezone issue. Whatever is accepted, I can submit a PR
Author
Owner

@ChMG commented on GitHub (Jan 25, 2023):

This is a good idea to make this more userfriendly.
I also prefer the first way.

Maybe there is an other variant of the first one.

  • Assume that when a user enters recurs every only, run the script immediately one time and then use the current datetime in the schedule at automatically.
@ChMG commented on GitHub (Jan 25, 2023): This is a good idea to make this more userfriendly. I also prefer the first way. Maybe there is an other variant of the first one. - Assume that when a user enters recurs every only, run the script immediately one time and then use the current datetime in the schedule at automatically.
Author
Owner

@abhi1693 commented on GitHub (Jan 25, 2023):

With my 1st suggestion that's what happens in the patch I have running in my NetBox. The schedule at is automatically set for the next run.

@abhi1693 commented on GitHub (Jan 25, 2023): With my 1st suggestion that's what happens in the patch I have running in my NetBox. The schedule at is automatically set for the next run.
Author
Owner

@kkthxbye-code commented on GitHub (Feb 2, 2023):

@abhi1693 - Fell free to submit a PR for the missing import and validation logic for checking that the scheduled time is set when interval is set.

A FR can be submitted for the other suggestion about defaulting to current time. I don't have any strong feelings about it, but I think we should get a fix out for scheduling being broken first.

@kkthxbye-code commented on GitHub (Feb 2, 2023): @abhi1693 - Fell free to submit a PR for the missing import and validation logic for checking that the scheduled time is set when interval is set. A FR can be submitted for the other suggestion about defaulting to current time. I don't have any strong feelings about it, but I think we should get a fix out for scheduling being broken first.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7554