Răsfoiți Sursa

Improved docs on background jobs on instances (#20489)

Jo 3 luni în urmă
părinte
comite
80f03daad6
1 a modificat fișierele cu 9 adăugiri și 1 ștergeri
  1. 9 1
      docs/plugins/development/background-jobs.md

+ 9 - 1
docs/plugins/development/background-jobs.md

@@ -60,6 +60,13 @@ Four of the standard Python logging levels are supported:
 
 Log entries recorded using the runner's logger will be saved in the job's log in the database in addition to being processed by other [system logging handlers](../../configuration/system.md#logging).
 
+### Jobs running for Model instances
+
+A Job can be executed for a specific instance of a Model.
+To enable this functionality, the model must include the `JobsMixin`.
+
+When enqueuing a Job, you can associate it with a particular instance by passing that instance to the `instance` parameter.
+
 ### Scheduled Jobs
 
 As described above, jobs can be scheduled for immediate execution or at any later time using the `enqueue()` method. However, for management purposes, the `enqueue_once()` method allows a job to be scheduled exactly once avoiding duplicates. If a job is already scheduled for a particular instance, a second one won't be scheduled, respecting thread safety. An example use case would be to schedule a periodic task that is bound to an instance in general, but not to any event of that instance (such as updates). The parameters of the `enqueue_once()` method are identical to those of `enqueue()`.
@@ -73,9 +80,10 @@ As described above, jobs can be scheduled for immediate execution or at any late
 from django.db import models
 from core.choices import JobIntervalChoices
 from netbox.models import NetBoxModel
+from netbox.models.features import JobsMixin
 from .jobs import MyTestJob
 
-class MyModel(NetBoxModel):
+class MyModel(JobsMixin, NetBoxModel):
     foo = models.CharField()
 
     def save(self, *args, **kwargs):