Skip to content

Commit

Permalink
Add predefined job interval choices
Browse files Browse the repository at this point in the history
  • Loading branch information
alehaa authored and jeremystretch committed Oct 11, 2024
1 parent 3e10d9a commit 8a8c328
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 7 additions & 2 deletions docs/plugins/development/background-jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -56,14 +59,15 @@ 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

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):
Expand All @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions netbox/core/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
3 changes: 2 additions & 1 deletion netbox/netbox/tests/dummy_plugin/jobs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8a8c328

Please sign in to comment.