-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clean_observers management commands
that cleans old subscriptions
- Loading branch information
1 parent
3b638ce
commit 9188a3e
Showing
4 changed files
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""".. Ignore pydocstyle D400. | ||
==================== | ||
Observers Management | ||
==================== | ||
.. automodule:: resolwe.observers.management.commands.register | ||
:members: | ||
""" |
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 @@ | ||
"""Management commands module.""" |
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,54 @@ | ||
""".. Ignore pydocstyle D400. | ||
======================= | ||
Clean old observer data | ||
======================= | ||
Command to run: | ||
./manage.py clean_observers | ||
""" | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from resolwe.observers.models import Observer, Subscription | ||
|
||
# Delete subscriptions older than DELETE_OLDER_THAN hours. | ||
DELETE_OLDER_THAN = 5 * 24 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Delete old observers subscriptions.""" | ||
|
||
help = "Delete old observer subscriptions." | ||
|
||
def add_arguments(self, parser): | ||
"""Command arguments.""" | ||
parser.add_argument( | ||
"--older-than", | ||
type=int, | ||
dest="older_than", | ||
help="Delete subscriptions older than older-than hours", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
"""Delete old observer subscriptions.""" | ||
delete_older_than = options["older_than"] or DELETE_OLDER_THAN | ||
logger.info( | ||
"Deleting observer subscriptions older than %d hours.", delete_older_than | ||
) | ||
# Delete subscriptions older than delete_older_than hours. | ||
deleted_subscriptions = Subscription.objects.filter( | ||
created__lt=timezone.now() - timezone.timedelta(hours=delete_older_than) | ||
).delete()[0] | ||
logger.info("Deleted %d subscriptions.", deleted_subscriptions) | ||
# Delete observers without subscriptions. | ||
deleted_observers = Observer.objects.filter( | ||
subscriptions__isnull=True | ||
).delete()[0] | ||
logger.info("Deleted %d observers.", deleted_observers) |