Add predefined job interval choices

This commit is contained in:
Alexander Haase 2024-10-09 22:14:49 +02:00 committed by Jeremy Stretch
parent 3e10d9a1da
commit 8a8c3287df
3 changed files with 23 additions and 3 deletions

View File

@ -29,6 +29,9 @@ class MyTestJob(JobRunner):
You can schedule the background job from within your code (e.g. from a model's `save()` method or a view) by calling `MyTestJob.enqueue()`. This method passes through all arguments to `Job.enqueue()`. However, no `name` argument must be passed, as the background job name will be used instead.
!!! tip
A set of predefined intervals can be used from `core.choices.JobIntervalChoices`.
### Attributes
`JobRunner` attributes are defined under a class named `Meta` within the job. These are optional (unless specified otherwise), but encouraged.
@ -56,6 +59,7 @@ As described above, jobs can be scheduled for immediate execution or at any late
```python title="models.py"
from django.db import models
from core.choices import JobIntervalChoices
from netbox.models import NetBoxModel
from .jobs import MyTestJob
@ -63,7 +67,7 @@ class MyModel(NetBoxModel):
foo = models.CharField()
def save(self, *args, **kwargs):
MyTestJob.enqueue_once(instance=self, interval=60)
MyTestJob.enqueue_once(instance=self, interval=JobIntervalChoices.INTERVAL_HOURLY)
return super().save(*args, **kwargs)
def sync(self):
@ -81,13 +85,14 @@ Some plugins may implement background jobs that are decoupled from any object an
#### Example
```python title="jobs.py"
from core.choices import JobIntervalChoices
from netbox.jobs import JobRunner
from .models import MyModel
class MyHousekeepingJob(JobRunner):
class Meta:
name = "My Housekeeping Job"
system_interval = 60 # every 60 minutes
system_interval = JobIntervalChoices.INTERVAL_HOURLY # or integer for n minutes
def run(self, *args, **kwargs):
MyModel.objects.filter(foo='bar').delete()

View File

@ -72,6 +72,20 @@ class JobStatusChoices(ChoiceSet):
)
class JobIntervalChoices(ChoiceSet):
INTERVAL_MINUTELY = 1
INTERVAL_HOURLY = 60
INTERVAL_DAILY = 60 * 24
INTERVAL_WEEKLY = 60 * 24 * 7
CHOICES = (
(INTERVAL_MINUTELY, _('Minutely')),
(INTERVAL_HOURLY, _('Hourly')),
(INTERVAL_DAILY, _('Daily')),
(INTERVAL_WEEKLY, _('Weekly')),
)
#
# ObjectChanges
#

View File

@ -1,9 +1,10 @@
from core.choices import JobIntervalChoices
from netbox.jobs import JobRunner
class DummySystemJob(JobRunner):
class Meta:
system_interval = 60
system_interval = JobIntervalChoices.INTERVAL_HOURLY
def run(self, *args, **kwargs):
pass