diff --git a/docs/plugins/development/background-jobs.md b/docs/plugins/development/background-jobs.md index 4e5cd18034..c1a313704b 100644 --- a/docs/plugins/development/background-jobs.md +++ b/docs/plugins/development/background-jobs.md @@ -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() diff --git a/netbox/core/choices.py b/netbox/core/choices.py index 01a072ce1c..442acc26ba 100644 --- a/netbox/core/choices.py +++ b/netbox/core/choices.py @@ -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 # diff --git a/netbox/netbox/tests/dummy_plugin/jobs.py b/netbox/netbox/tests/dummy_plugin/jobs.py index f0934aa966..bf6560fd6e 100644 --- a/netbox/netbox/tests/dummy_plugin/jobs.py +++ b/netbox/netbox/tests/dummy_plugin/jobs.py @@ -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