Running scripts with commit = False makes the job fail in the background #7456

Closed
opened 2025-12-29 20:23:39 +01:00 by adam · 2 comments
Owner

Originally created by @kkthxbye-code on GitHub (Jan 5, 2023).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.4.2

Python version

3.10

Steps to Reproduce

  1. Create a test script
from extras.scripts import Script, StringVar
from tenancy.models import Tenant

class Script0(Script):
    def run(self, data, commit):
        tenant = Tenant(name="test", slug="test")

        tenant.save()
  1. Run the script and mark commit as false

Expected Behavior

The rqworker should not throw an exception and the job should not show up in "Failed jobs" in the admin page.

Observed Behavior

The rqworker will throw the following exception:

Traceback (most recent call last):
  File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/rq/worker.py", line 1075, in perform_job
    rv = job.perform()
  File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 854, in perform
    self._result = self._execute()
  File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 877, in _execute
    result = self.func(*self.args, **self.kwargs)
  File "/home/main/devel/repos/netbox/netbox/extras/scripts.py", line 494, in run_script
    _run_script()
  File "/home/main/devel/repos/netbox/netbox/extras/scripts.py", line 472, in _run_script
    clear_webhooks.send(request)
  File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/main/devel/repos/netbox/netbox/extras/signals.py", line 131, in clear_webhook_queue
    logger.info(f"Clearing {len(webhooks_queue.get())} queued webhooks ({sender})")
LookupError: <ContextVar name='webhooks_queue' at 0x7f446427acf0>

The same exception can be seen in the admin panel when watching the queues.

Seems to be caused by this: https://github.com/netbox-community/netbox/pull/10883

Can you take a look at this @jeremystretch - not sure I understand what the exact issue is.

Originally created by @kkthxbye-code on GitHub (Jan 5, 2023). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.4.2 ### Python version 3.10 ### Steps to Reproduce 1. Create a test script ``` from extras.scripts import Script, StringVar from tenancy.models import Tenant class Script0(Script): def run(self, data, commit): tenant = Tenant(name="test", slug="test") tenant.save() ``` 2. Run the script and mark commit as false ### Expected Behavior The rqworker should not throw an exception and the job should not show up in "Failed jobs" in the admin page. ### Observed Behavior The rqworker will throw the following exception: ``` Traceback (most recent call last): File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/rq/worker.py", line 1075, in perform_job rv = job.perform() File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 854, in perform self._result = self._execute() File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 877, in _execute result = self.func(*self.args, **self.kwargs) File "/home/main/devel/repos/netbox/netbox/extras/scripts.py", line 494, in run_script _run_script() File "/home/main/devel/repos/netbox/netbox/extras/scripts.py", line 472, in _run_script clear_webhooks.send(request) File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 176, in send return [ File "/home/main/devel/repos/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp> (receiver, receiver(signal=self, sender=sender, **named)) File "/home/main/devel/repos/netbox/netbox/extras/signals.py", line 131, in clear_webhook_queue logger.info(f"Clearing {len(webhooks_queue.get())} queued webhooks ({sender})") LookupError: <ContextVar name='webhooks_queue' at 0x7f446427acf0> ``` The same exception can be seen in the admin panel when watching the queues. Seems to be caused by this: https://github.com/netbox-community/netbox/pull/10883 Can you take a look at this @jeremystretch - not sure I understand what the exact issue is.
adam added the type: bugstatus: accepted labels 2025-12-29 20:23:39 +01:00
adam closed this issue 2025-12-29 20:23:39 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 5, 2023):

I think the issue is that we're not setting a default on the webhooks queue, so we get a lookup error if the queue has never been explicitly defined. Should be simple enough to clean up but I'll need to do some testing.

@jeremystretch commented on GitHub (Jan 5, 2023): I think the issue is that we're not setting a default on the webhooks queue, so we get a lookup error if the queue has never been explicitly defined. Should be simple enough to clean up but I'll need to do some testing.
Author
Owner

@kkthxbye-code commented on GitHub (Jan 10, 2023):

Thanks for the fix. I verified that it allows scripts with commit = False to not error and that no webhooks are generated.

@kkthxbye-code commented on GitHub (Jan 10, 2023): Thanks for the fix. I verified that it allows scripts with commit = False to not error and that no webhooks are generated.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7456