netbox icon indicating copy to clipboard operation
netbox copied to clipboard

Job.clean() not called

Open alehaa opened this issue 1 year ago • 8 comments

Deployment Type

Self-hosted

NetBox Version

v4.1.3

Python Version

3.11

Steps to Reproduce

  1. Create a new JobRunner.
  2. Schedule the job runner without with enqueue(interval=0).

Expected Behavior

Getting a ValidationError:

django.core.exceptions.ValidationError: {
  '__all__': [
    'Jobs cannot be assigned to this object type (None).'
  ]
}

Observed Behavior

Nothing happens, as neither Job.enqueue(), Job.start() nor Job.terminate() trigger Job.clean().

alehaa avatar Oct 07 '24 11:10 alehaa

This error occured during testing of similar issues, as Job.clean() (like #17682, #17086 and #17648) assumes the object_type field is not None. If its decided Job should call clean(), this should be fixed as well (could happen in conjunction with #17682). Otherwise the method could be removed IMHO.

alehaa avatar Oct 07 '24 11:10 alehaa

@alehaa could you please elaborate on your steps to reproduce above? It is not clear exactly what you're doing. Example code would be helpful.

jeremystretch avatar Oct 07 '24 12:10 jeremystretch

During the implementation of #16971, I tested whether NetBox would raise exceptions on incorrectly configured jobs, and whether separate tests needed to be added to the system job registration. I added this (abbreviated) code to NetBox:

class SampleJob(JobRunner):
    def run(self, *args, **kwargs):
        pass

SampleJob.enqueue_once(interval=0)

The job can be scheduled without error. However, this should fail for two reasons:

  1. interval=0 violates the field's MinValueValidator(1).
  2. As no instance is set, clean() would raise an exception as it doesn't take into account self.object_type can be None. That's a bug related to #17682, #17086 and #17648, but didn't attract attention before, as clean() isn't called by any of the other methods in Job.

As this is not a cause of system jobs (#16971), but the Job model itself, I see this as a separate issue / bug.

alehaa avatar Oct 07 '24 12:10 alehaa

This is a reminder that additional information is needed in order to further triage this issue. If the requested details are not provided, the issue will soon be closed automatically.

github-actions[bot] avatar Oct 15 '24 04:10 github-actions[bot]

@jeremystretch Did you have a chance to review the additional information above?

alehaa avatar Oct 15 '24 05:10 alehaa

@alehaa pk, I'll flag this for an owner, unless you'd like to take it?

jeremystretch avatar Oct 22 '24 14:10 jeremystretch

I can provide a PR. As this is a fix for existing functionality I assume develop is the right branch?

alehaa avatar Oct 22 '24 16:10 alehaa

@alehaa great, thanks! And yeah, develop please.

jeremystretch avatar Oct 22 '24 16:10 jeremystretch