mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-31 21:06:25 -06:00
Add predefined job interval choices
This commit is contained in:
parent
3e10d9a1da
commit
8a8c3287df
@ -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.
|
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
|
### Attributes
|
||||||
|
|
||||||
`JobRunner` attributes are defined under a class named `Meta` within the job. These are optional (unless specified otherwise), but encouraged.
|
`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"
|
```python title="models.py"
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from core.choices import JobIntervalChoices
|
||||||
from netbox.models import NetBoxModel
|
from netbox.models import NetBoxModel
|
||||||
from .jobs import MyTestJob
|
from .jobs import MyTestJob
|
||||||
|
|
||||||
@ -63,7 +67,7 @@ class MyModel(NetBoxModel):
|
|||||||
foo = models.CharField()
|
foo = models.CharField()
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
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)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
def sync(self):
|
def sync(self):
|
||||||
@ -81,13 +85,14 @@ Some plugins may implement background jobs that are decoupled from any object an
|
|||||||
#### Example
|
#### Example
|
||||||
|
|
||||||
```python title="jobs.py"
|
```python title="jobs.py"
|
||||||
|
from core.choices import JobIntervalChoices
|
||||||
from netbox.jobs import JobRunner
|
from netbox.jobs import JobRunner
|
||||||
from .models import MyModel
|
from .models import MyModel
|
||||||
|
|
||||||
class MyHousekeepingJob(JobRunner):
|
class MyHousekeepingJob(JobRunner):
|
||||||
class Meta:
|
class Meta:
|
||||||
name = "My Housekeeping Job"
|
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):
|
def run(self, *args, **kwargs):
|
||||||
MyModel.objects.filter(foo='bar').delete()
|
MyModel.objects.filter(foo='bar').delete()
|
||||||
|
@ -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
|
# ObjectChanges
|
||||||
#
|
#
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
|
from core.choices import JobIntervalChoices
|
||||||
from netbox.jobs import JobRunner
|
from netbox.jobs import JobRunner
|
||||||
|
|
||||||
|
|
||||||
class DummySystemJob(JobRunner):
|
class DummySystemJob(JobRunner):
|
||||||
class Meta:
|
class Meta:
|
||||||
system_interval = 60
|
system_interval = JobIntervalChoices.INTERVAL_HOURLY
|
||||||
|
|
||||||
def run(self, *args, **kwargs):
|
def run(self, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user