This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added NETJSONGRAPH_LINK_EXPIRATION days setting
- Loading branch information
1 parent
f90c639
commit d6fff61
Showing
4 changed files
with
89 additions
and
16 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
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 |
---|---|---|
@@ -1,20 +1,48 @@ | ||
import sys | ||
|
||
from datetime import timedelta | ||
from django.utils.timezone import now | ||
|
||
from .contextmanagers import log_on_fail | ||
from .models import Topology | ||
from .models import Topology, Link | ||
from .settings import LINK_EXPIRATION | ||
|
||
|
||
def print_info(message): # pragma no cover | ||
""" | ||
print info message if calling from management command ``update_topology`` | ||
""" | ||
if 'update_topology' in sys.argv: | ||
print('{0}\n'.format(message)) | ||
|
||
|
||
def delete_expired_links(): | ||
""" | ||
deletes links that have been down for more than | ||
``NETJSONGRAPH_LINK_EXPIRATION`` days | ||
""" | ||
if LINK_EXPIRATION not in [False, None]: | ||
expiration_date = now() - timedelta(days=int(LINK_EXPIRATION)) | ||
expired_links = Link.objects.filter(status='down', | ||
modified__lt=expiration_date) | ||
expired_links_length = len(expired_links) | ||
if expired_links_length: | ||
print_info('Deleting {0} expired links'.format(expired_links_length)) | ||
for link in expired_links: | ||
link.delete() | ||
|
||
|
||
def update_topology(label=None): | ||
""" | ||
updates all the topology | ||
sends logs to the "nodeshot.networking" logger | ||
- updates topologies | ||
- logs failures | ||
- calls delete_expired_links() | ||
""" | ||
queryset = Topology.objects.filter(published=True) | ||
if label: | ||
queryset = queryset.filter(label__icontains=label) | ||
for topology in queryset: | ||
# print info message if calling from management command | ||
if 'update_topology' in sys.argv: # pragma no cover | ||
print('Updating topology {0}\n'.format(topology)) | ||
print_info('Updating topology {0}'.format(topology)) | ||
with log_on_fail('update', topology): | ||
topology.update() | ||
delete_expired_links() |