Add background jobs to plugin API #15692
Labels
complexity: high
Expected to require a large amont of time and effort to implement relative to other tasks
status: under review
Further discussion is needed to determine this issue's scope and/or implementation
type: feature
Introduction of new functionality to the application
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: