Skip to content

Commit bf88f18

Browse files
alehaajeremystretch
authored andcommitted
Add predefined job interval choices
1 parent 274a5b4 commit bf88f18

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

docs/plugins/development/background-jobs.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class MyTestJob(JobRunner):
2929

3030
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.
3131

32+
!!! tip
33+
A set of predefined intervals can be used from `core.choices.JobIntervalChoices`.
34+
3235
### Attributes
3336

3437
`JobRunner` attributes are defined under a class named `Meta` within the job. These are optional (unless specified otherwise), but encouraged.
@@ -56,14 +59,15 @@ As described above, jobs can be scheduled for immediate execution or at any late
5659

5760
```python title="models.py"
5861
from django.db import models
62+
from core.choices import JobIntervalChoices
5963
from netbox.models import NetBoxModel
6064
from .jobs import MyTestJob
6165

6266
class MyModel(NetBoxModel):
6367
foo = models.CharField()
6468

6569
def save(self, *args, **kwargs):
66-
MyTestJob.enqueue_once(instance=self, interval=60)
70+
MyTestJob.enqueue_once(instance=self, interval=JobIntervalChoices.INTERVAL_HOURLY)
6771
return super().save(*args, **kwargs)
6872

6973
def sync(self):
@@ -81,13 +85,14 @@ Some plugins may implement background jobs that are decoupled from any object an
8185
#### Example
8286

8387
```python title="jobs.py"
88+
from core.choices import JobIntervalChoices
8489
from netbox.jobs import JobRunner
8590
from .models import MyModel
8691

8792
class MyHousekeepingJob(JobRunner):
8893
class Meta:
8994
name = "My Housekeeping Job"
90-
system_interval = 60 # every 60 minutes
95+
system_interval = JobIntervalChoices.INTERVAL_HOURLY # or integer for n minutes
9196

9297
def run(self, *args, **kwargs):
9398
MyModel.objects.filter(foo='bar').delete()

netbox/core/choices.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ class JobStatusChoices(ChoiceSet):
7272
)
7373

7474

75+
class JobIntervalChoices(ChoiceSet):
76+
INTERVAL_MINUTELY = 1
77+
INTERVAL_HOURLY = 60
78+
INTERVAL_DAILY = 60 * 24
79+
INTERVAL_WEEKLY = 60 * 24 * 7
80+
81+
CHOICES = (
82+
(INTERVAL_MINUTELY, _('Minutely')),
83+
(INTERVAL_HOURLY, _('Hourly')),
84+
(INTERVAL_DAILY, _('Daily')),
85+
(INTERVAL_WEEKLY, _('Weekly')),
86+
)
87+
88+
7589
#
7690
# ObjectChanges
7791
#

netbox/netbox/tests/dummy_plugin/jobs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from core.choices import JobIntervalChoices
12
from netbox.jobs import JobRunner
23

34

45
class DummySystemJob(JobRunner):
56
class Meta:
6-
system_interval = 60
7+
system_interval = JobIntervalChoices.INTERVAL_HOURLY
78

89
def run(self, *args, **kwargs):
910
pass

0 commit comments

Comments
 (0)