Skip to content

Commit e459805

Browse files
author
kkthxbye
committed
Add dynamic config JOBRESULT_RETENTION
and cleanup functionality to the housekeeping script
1 parent 086c46c commit e459805

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

docs/administration/housekeeping.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ NetBox includes a `housekeeping` management command that should be run nightly.
44

55
* Clearing expired authentication sessions from the database
66
* Deleting changelog records older than the configured [retention time](../configuration/dynamic-settings.md#changelog_retention)
7+
* Deleting job result records older than the configured [retention time](../configuration/dynamic-settings.md#jobresult_retention)
78

89
This command can be invoked directly, or by using the shell script provided at `/opt/netbox/contrib/netbox-housekeeping.sh`. This script can be linked from your cron scheduler's daily jobs directory (e.g. `/etc/cron.daily`) or referenced directly within the cron configuration file.
910

docs/configuration/dynamic-settings.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ changes in the database indefinitely.
4343

4444
---
4545

46+
## JOBRESULT_RETENTION
47+
48+
Default: 0
49+
50+
The number of days to retain job results (scripts and reports). Set this to `0` to retain
51+
job results in the database indefinitely.
52+
53+
!!! warning
54+
If enabling indefinite job results retention, it is recommended to periodically delete old entries. Otherwise, the database may eventually exceed capacity.
55+
56+
---
57+
4658
## CUSTOM_VALIDATORS
4759

4860
This is a mapping of models to [custom validators](../customization/custom-validation.md) that have been defined locally to enforce custom validation logic. An example is provided below:

netbox/extras/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ConfigRevisionAdmin(admin.ModelAdmin):
3434
'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
3535
}),
3636
('Miscellaneous', {
37-
'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'MAPS_URL'),
37+
'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'JOBRESULT_RETENTION', 'MAPS_URL'),
3838
}),
3939
('Config Revision', {
4040
'fields': ('comment',),

netbox/extras/management/commands/housekeeping.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.utils import timezone
1010
from packaging import version
1111

12+
from extras.models import JobResult
1213
from extras.models import ObjectChange
1314
from netbox.config import Config
1415

@@ -63,6 +64,33 @@ def handle(self, *args, **options):
6364
f"\tSkipping: No retention period specified (CHANGELOG_RETENTION = {config.CHANGELOG_RETENTION})"
6465
)
6566

67+
# Delete expired JobResults
68+
if options['verbosity']:
69+
self.stdout.write("[*] Checking for expired jobresult records")
70+
if config.JOBRESULT_RETENTION:
71+
cutoff = timezone.now() - timedelta(days=config.JOBRESULT_RETENTION)
72+
if options['verbosity'] >= 2:
73+
self.stdout.write(f"\tRetention period: {config.JOBRESULT_RETENTION} days")
74+
self.stdout.write(f"\tCut-off time: {cutoff}")
75+
expired_records = JobResult.objects.filter(created__lt=cutoff).count()
76+
if expired_records:
77+
if options['verbosity']:
78+
self.stdout.write(
79+
f"\tDeleting {expired_records} expired records... ",
80+
self.style.WARNING,
81+
ending=""
82+
)
83+
self.stdout.flush()
84+
JobResult.objects.filter(created__lt=cutoff)._raw_delete(using=DEFAULT_DB_ALIAS)
85+
if options['verbosity']:
86+
self.stdout.write("Done.", self.style.SUCCESS)
87+
elif options['verbosity']:
88+
self.stdout.write("\tNo expired records found.", self.style.SUCCESS)
89+
elif options['verbosity']:
90+
self.stdout.write(
91+
f"\tSkipping: No retention period specified (JOBRESULT_RETENTION = {config.JOBRESULT_RETENTION})"
92+
)
93+
6694
# Check for new releases (if enabled)
6795
if options['verbosity']:
6896
self.stdout.write("[*] Checking for latest release")

netbox/netbox/config/parameters.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ def __init__(self, name, label, default, description='', field=None, field_kwarg
162162
description="Days to retain changelog history (set to zero for unlimited)",
163163
field=forms.IntegerField
164164
),
165+
ConfigParam(
166+
name='JOBRESULT_RETENTION',
167+
label='Job result retention',
168+
default=0,
169+
description="Days to retain job result history (set to zero for unlimited)",
170+
field=forms.IntegerField
171+
),
165172
ConfigParam(
166173
name='MAPS_URL',
167174
label='Maps URL',

0 commit comments

Comments
 (0)