-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #6590: Introduce a nightly housekeeping command to clear expir…
…ed sessions and change records
- Loading branch information
1 parent
a1ba3b5
commit d87ec82
Showing
8 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
# This shell script invokes NetBox's housekeeping management command, which | ||
# intended to be run nightly. This script can be copied into your system's | ||
# daily cron directory (e.g. /etc/cron.daily), or referenced directly from | ||
# within the cron configuration file. | ||
# | ||
# If NetBox has been installed into a nonstandard location, update the paths | ||
# below. | ||
/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py housekeeping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Housekeeping | ||
|
||
NetBox includes a `housekeeping` management command that should be run nightly. This command handles: | ||
|
||
* Clearing expired authentication sessions from the database | ||
* Deleting changelog records older than the configured [retention time](../configuration/optional-settings.md#changelog_retention) | ||
|
||
This command can be invoked directly, or by using the shell script provided at `/opt/netbox/contrib/netbox-housekeeping.sh`. This script can be copied into your cron scheduler's daily jobs directory (e.g. `/etc/cron.daily`) or referenced directly within the cron configuration file. | ||
|
||
The `housekeeping` command can also be run manually at any time: Running the command outside of scheduled execution times will not interfere with its operation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from datetime import timedelta | ||
from importlib import import_module | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from django.db import DEFAULT_DB_ALIAS | ||
from django.utils import timezone | ||
|
||
from extras.models import ObjectChange | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Perform nightly housekeeping tasks. (This command can be run at any time.)" | ||
|
||
def handle(self, *args, **options): | ||
|
||
# Clear expired authentication sessions (essentially replicating the `clearsessions` command) | ||
self.stdout.write("[*] Clearing expired authentication sessions") | ||
if options['verbosity'] >= 2: | ||
self.stdout.write(f"\tConfigured session engine: {settings.SESSION_ENGINE}") | ||
engine = import_module(settings.SESSION_ENGINE) | ||
try: | ||
engine.SessionStore.clear_expired() | ||
self.stdout.write("\tSessions cleared.", self.style.SUCCESS) | ||
except NotImplementedError: | ||
self.stdout.write( | ||
f"\tThe configured session engine ({settings.SESSION_ENGINE}) does not support " | ||
f"clearing sessions; skipping." | ||
) | ||
|
||
# Delete expired ObjectRecords | ||
self.stdout.write("[*] Checking for expired changelog records") | ||
if settings.CHANGELOG_RETENTION: | ||
cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION) | ||
if options['verbosity'] >= 2: | ||
self.stdout.write(f"Retention period: {settings.CHANGELOG_RETENTION} days") | ||
self.stdout.write(f"\tCut-off time: {cutoff}") | ||
expired_records = ObjectChange.objects.filter(time__lt=cutoff).count() | ||
if expired_records: | ||
self.stdout.write(f"\tDeleting {expired_records} expired records... ", self.style.WARNING, ending="") | ||
self.stdout.flush() | ||
ObjectChange.objects.filter(time__lt=cutoff)._raw_delete(using=DEFAULT_DB_ALIAS) | ||
self.stdout.write("Done.", self.style.WARNING) | ||
else: | ||
self.stdout.write("\tNo expired records found.") | ||
else: | ||
self.stdout.write( | ||
f"\tSkipping: No retention period specified (CHANGELOG_RETENTION = {settings.CHANGELOG_RETENTION})" | ||
) | ||
|
||
self.stdout.write("Finished.", self.style.SUCCESS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters