-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add background jobs to plugin API #15692
Comments
I think it would be very useful if netbox support easier usage of the jobs for plugins. A lot of plugins implement a custom solution with the big disadvantage that the jobs not listed in the Jobs Tab. |
@alehaa thanks for the detailed FR and example code! We've selected this as a milestone initiative for NetBox v4.1. I was set to work on it myself, but would you like to contribute a PR for it? |
@jeremystretch sure I can work on it. |
@alehaa great! Please be sure to base your PR off of the |
@alehaa I have done similar things in my plugins to achieve similar results. If you want we can collaborate on this feature in your fork. Just let me know. I'm also on slack with the same username if you want to reach out. Thanks for your time :) |
@alehaa can you share what progress you've made on this? |
@jeremystretch Unfortunately I didn't start yet but plan on submitting a PR this week. |
Despite the missing documentation pages, the However, for adding |
@alehaa please submit what you have for now as a draft PR and I'll take a look. Thanks! |
@jeremystretch I've submitted a new draft PR related to this issue. Additional |
After giving it some thought, I see no built-in option for this in Django and some custom logic will need to be used to solve this. I could imagine sending a signal just before the WSGI application accepts requests to set up background jobs. Would this be acceptable and in line with the general NetBox codebase? |
I've found a solution that works without introducing new methods or custom signals by reusing Django's |
NetBox version
v3.7.5
Feature type
Change to existing functionality
Proposed functionality
NetBox already includes the functionality to schedule jobs using either the
django-rq
library or thecore.Job
model combined with theJobsMixin
class. Making the abstract job functionality publicly available to plugins allows decoupling from the backend ofdjango-rq
and a consistent experience in NetBox across all functionality. For this I propose:Add
netbox.models.JobsMixin
to the list of API available model mixins in the documentation. This allows plugins to implement new models with Jobs enabled.Add a new
BackgroundJob
class to implement the execution of the job's code, i.e. for consistency in callingstart
,terminate
and setting error messages. This class should also be used in existing NetBox functionality to run background jobs for consistency. Below is a sample implementation from a plugin of mine for demonstration purposes.Optional: Enhance the
core.models.Job.enqueue()
method with aqueue
parameter to schedule jobs for specific queues (e.g.low
,high
). If not provided the default queue will be used, so there's no change in API compatibility.Optional: Add the ability to schedule system background tasks by plugins, e.g. for periodic synchronization with other systems. Below is a sample implementation from a plugin of mine for demonstration purposes.
Use case
Plugins get a standardized interface for adding models with jobs enabled using the
JobsMixin
, just like native NetBox models. This provides a consistent experience.The environment for running background jobs will be standardized, as startup, termination, and error handling will be the same for all jobs. Individual jobs don't have to worry about rescheduling, but can rely on well-tested and managed code.
Using the
SystemJob
interface, plugins could schedule system tasks such as periodic synchronization with other systems (e.g. virtualization clusters) or perform housekeeping. These jobs are usually not bound to a specific NetBox object and currently require either direct access to thedjango-rq
library or use of an external cronjob and management commands.Database changes
None
External dependencies
None
For the functionality described above I can share my existing code, add test cases and provide a PR for review. Special thanks goes to @wouterdebruijn for sharing his ideas and feedback in the NetBox discussions.
The text was updated successfully, but these errors were encountered: