When a background job is scheduled without an instance, delete() on the job model will throw exceptions #10311

Closed
opened 2025-12-29 21:29:47 +01:00 by adam · 3 comments
Owner

Originally created by @DanSheps on GitHub (Sep 30, 2024).

Originally assigned to: @DanSheps on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.1.2

Python Version

3.12

Steps to Reproduce

  1. Create a JobRunner class
  2. Do not assign an object to the job instance
  3. Add the JobRunner.enqueue_once to the ready() of a plugin

Expected Behavior

NetBox will function normally

Observed Behavior

AttributeError is raised in Job.delete()

Originally created by @DanSheps on GitHub (Sep 30, 2024). Originally assigned to: @DanSheps on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.1.2 ### Python Version 3.12 ### Steps to Reproduce 1. Create a JobRunner class 2. Do not assign an object to the job instance 3. Add the JobRunner.enqueue_once to the ready() of a plugin ### Expected Behavior NetBox will function normally ### Observed Behavior AttributeError is raised in `Job.delete()`
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 21:29:47 +01:00
adam closed this issue 2025-12-29 21:29:47 +01:00
Author
Owner

@alehaa commented on GitHub (Sep 30, 2024):

I believe this is because delete() doesn't check if object_type is None.

rq_queue_name = get_config().QUEUE_MAPPINGS.get(self.object_type.model, RQ_QUEUE_DEFAULT)

In enqueue() this is handled by:

rq_queue_name = get_queue_for_model(object_type.model if object_type else None)

I suggest using the same lookup in both instances and add to add a test checking jobs actually can be deleted. If this is accepted, I volunteer for a PR.

@alehaa commented on GitHub (Sep 30, 2024): I believe this is because `delete()` doesn't check if `object_type` is `None`. ```Python rq_queue_name = get_config().QUEUE_MAPPINGS.get(self.object_type.model, RQ_QUEUE_DEFAULT) ``` In `enqueue()` this is handled by: ```Python rq_queue_name = get_queue_for_model(object_type.model if object_type else None) ``` I suggest using the same lookup in both instances and add to add a test checking jobs actually can be deleted. If this is accepted, I volunteer for a PR.
Author
Owner

@DanSheps commented on GitHub (Oct 1, 2024):

Yup, I knew the problem, but thanks for pointing out the get_queue_for_model method.

@DanSheps commented on GitHub (Oct 1, 2024): Yup, I knew the problem, but thanks for pointing out the `get_queue_for_model` method.
Author
Owner

@alehaa commented on GitHub (Oct 7, 2024):

It seems that deleting jobs is already covered by a test case (test_enqueue_once_twice_different_schedule_at internally triggers a job.delete()). However, this only applies to jobs with an instance set. I'll add tests for jobs without instances during implementation of #16971, so this will be covered as well.

@alehaa commented on GitHub (Oct 7, 2024): It seems that deleting jobs is already covered by a test case (`test_enqueue_once_twice_different_schedule_at` internally triggers a `job.delete()`). However, this only applies to jobs with an `instance` set. I'll add tests for jobs without instances during implementation of #16971, so this will be covered as well.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10311