Skip to content

Commit 7de47b8

Browse files
jeremystretchbctiemann
authored andcommitted
Closes #18362: Create a system job for census reporting
1 parent 5e35ca6 commit 7de47b8

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

netbox/core/jobs.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2+
import requests
3+
import sys
24

3-
from netbox.jobs import JobRunner
5+
from django.conf import settings
6+
from netbox.jobs import JobRunner, system_job
47
from netbox.search.backends import search_backend
5-
from .choices import DataSourceStatusChoices
8+
from .choices import DataSourceStatusChoices, JobIntervalChoices
69
from .exceptions import SyncError
710
from .models import DataSource
811

@@ -31,3 +34,44 @@ def run(self, *args, **kwargs):
3134
if type(e) is SyncError:
3235
logging.error(e)
3336
raise e
37+
38+
39+
@system_job(interval=JobIntervalChoices.INTERVAL_DAILY)
40+
class SystemHousekeepingJob(JobRunner):
41+
"""
42+
Perform daily system housekeeping functions.
43+
"""
44+
class Meta:
45+
name = "System Housekeeping"
46+
47+
def run(self, *args, **kwargs):
48+
# Skip if running in development or test mode
49+
if settings.DEBUG or 'test' in sys.argv:
50+
return
51+
52+
# TODO: Migrate other housekeeping functions from the `housekeeping` management command.
53+
self.send_census_report()
54+
55+
@staticmethod
56+
def send_census_report():
57+
"""
58+
Send a census report (if enabled).
59+
"""
60+
# Skip if census reporting is disabled
61+
if settings.ISOLATED_DEPLOYMENT or not settings.CENSUS_REPORTING_ENABLED:
62+
return
63+
64+
census_data = {
65+
'version': settings.RELEASE.full_version,
66+
'python_version': sys.version.split()[0],
67+
'deployment_id': settings.DEPLOYMENT_ID,
68+
}
69+
try:
70+
requests.get(
71+
url=settings.CENSUS_URL,
72+
params=census_data,
73+
timeout=3,
74+
proxies=settings.HTTP_PROXIES
75+
)
76+
except requests.exceptions.RequestException:
77+
pass

netbox/netbox/settings.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import platform
66
import sys
77
import warnings
8-
from urllib.parse import urlencode
98

10-
import requests
119
from django.contrib.messages import constants as messages
1210
from django.core.exceptions import ImproperlyConfigured, ValidationError
1311
from django.core.validators import URLValidator
@@ -583,17 +581,6 @@ def _setting(name, default=None):
583581
# Calculate a unique deployment ID from the secret key
584582
DEPLOYMENT_ID = hashlib.sha256(SECRET_KEY.encode('utf-8')).hexdigest()[:16]
585583
CENSUS_URL = 'https://census.netbox.oss.netboxlabs.com/api/v1/'
586-
CENSUS_PARAMS = {
587-
'version': RELEASE.full_version,
588-
'python_version': sys.version.split()[0],
589-
'deployment_id': DEPLOYMENT_ID,
590-
}
591-
if CENSUS_REPORTING_ENABLED and not ISOLATED_DEPLOYMENT and not DEBUG and 'test' not in sys.argv:
592-
try:
593-
# Report anonymous census data
594-
requests.get(f'{CENSUS_URL}?{urlencode(CENSUS_PARAMS)}', timeout=3, proxies=HTTP_PROXIES)
595-
except requests.exceptions.RequestException:
596-
pass
597584

598585

599586
#

0 commit comments

Comments
 (0)