mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-30 04:16:24 -06:00
Specify interval when registering system job
This commit is contained in:
parent
566201507d
commit
6b192c9ae8
@ -17,11 +17,13 @@ class Command(_Command):
|
|||||||
"""
|
"""
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
# Setup system jobs.
|
# Setup system jobs.
|
||||||
for job in registry['system_jobs'].values():
|
for job, kwargs in registry['system_jobs'].items():
|
||||||
interval = getattr(job.Meta, 'system_interval', 0)
|
try:
|
||||||
if interval:
|
interval = kwargs['interval']
|
||||||
logger.debug(f"Scheduling system job {job.name}")
|
except KeyError:
|
||||||
job.enqueue_once(interval=interval)
|
raise TypeError("System job must specify an interval (in minutes).")
|
||||||
|
logger.debug(f"Scheduling system job {job.name} (interval={interval})")
|
||||||
|
job.enqueue_once(**kwargs)
|
||||||
|
|
||||||
# Run the worker with scheduler functionality
|
# Run the worker with scheduler functionality
|
||||||
options['with_scheduler'] = True
|
options['with_scheduler'] = True
|
||||||
|
@ -2,6 +2,7 @@ import logging
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.utils.functional import classproperty
|
from django.utils.functional import classproperty
|
||||||
from django_pglocks import advisory_lock
|
from django_pglocks import advisory_lock
|
||||||
from rq.timeouts import JobTimeoutException
|
from rq.timeouts import JobTimeoutException
|
||||||
@ -17,12 +18,17 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def system_job():
|
def system_job(interval):
|
||||||
"""
|
"""
|
||||||
Decorator for registering a `JobRunner` class as system background job.
|
Decorator for registering a `JobRunner` class as system background job.
|
||||||
"""
|
"""
|
||||||
|
if type(interval) is not int:
|
||||||
|
raise ImproperlyConfigured("System job interval must be an integer (minutes).")
|
||||||
|
|
||||||
def _wrapper(cls):
|
def _wrapper(cls):
|
||||||
registry['system_jobs'][cls.name] = cls
|
registry['system_jobs'][cls] = {
|
||||||
|
'interval': interval
|
||||||
|
}
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
return _wrapper
|
return _wrapper
|
||||||
|
@ -2,15 +2,8 @@ from core.choices import JobIntervalChoices
|
|||||||
from netbox.jobs import JobRunner, system_job
|
from netbox.jobs import JobRunner, system_job
|
||||||
|
|
||||||
|
|
||||||
@system_job()
|
@system_job(interval=JobIntervalChoices.INTERVAL_HOURLY)
|
||||||
class DummySystemJob(JobRunner):
|
class DummySystemJob(JobRunner):
|
||||||
class Meta:
|
|
||||||
system_interval = JobIntervalChoices.INTERVAL_HOURLY
|
|
||||||
|
|
||||||
def run(self, *args, **kwargs):
|
def run(self, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
system_jobs = (
|
|
||||||
DummySystemJob,
|
|
||||||
)
|
|
||||||
|
@ -5,6 +5,7 @@ from django.core.exceptions import ImproperlyConfigured
|
|||||||
from django.test import Client, TestCase, override_settings
|
from django.test import Client, TestCase, override_settings
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from core.choices import JobIntervalChoices
|
||||||
from netbox.tests.dummy_plugin import config as dummy_config
|
from netbox.tests.dummy_plugin import config as dummy_config
|
||||||
from netbox.tests.dummy_plugin.data_backends import DummyBackend
|
from netbox.tests.dummy_plugin.data_backends import DummyBackend
|
||||||
from netbox.tests.dummy_plugin.jobs import DummySystemJob
|
from netbox.tests.dummy_plugin.jobs import DummySystemJob
|
||||||
@ -135,8 +136,8 @@ class PluginTest(TestCase):
|
|||||||
"""
|
"""
|
||||||
Check registered system jobs.
|
Check registered system jobs.
|
||||||
"""
|
"""
|
||||||
self.assertIn(DummySystemJob.name, registry['system_jobs'])
|
self.assertIn(DummySystemJob, registry['system_jobs'])
|
||||||
self.assertIs(registry['system_jobs'][DummySystemJob.name], DummySystemJob)
|
self.assertEqual(registry['system_jobs'][DummySystemJob]['interval'], JobIntervalChoices.INTERVAL_HOURLY)
|
||||||
|
|
||||||
def test_queues(self):
|
def test_queues(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user