Skip to content

Commit

Permalink
Merge pull request #186 from akolson/add-cleanupsyncs-params
Browse files Browse the repository at this point in the history
Adds cleanupsyncs command parameters
  • Loading branch information
bjester authored Jul 18, 2023
2 parents fd882f6 + 89fac4c commit 5ef2225
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Release Notes

List of the most important changes for each release.
## 0.16.17
- Added `client-instance-id`, `server-instance-id`, `sync-filter`, `push` and `pull` arguments to `cleanupsyncs` management command

## 0.6.16
- Added dedicated `client_instance_id` and `server_instance_id` fields to `SyncSession`
Expand Down
2 changes: 1 addition & 1 deletion morango/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from __future__ import unicode_literals

default_app_config = "morango.apps.MorangoConfig"
__version__ = "0.6.16"
__version__ = "0.6.17"
46 changes: 46 additions & 0 deletions morango/management/commands/cleanupsyncs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
import uuid

from django.core.management.base import BaseCommand
from django.db import transaction
Expand Down Expand Up @@ -29,6 +30,36 @@ def add_arguments(self, parser):
default=6,
help="Number of hours of inactivity after which a session should be considered stale",
)
parser.add_argument(
"--client-instance-id",
type=uuid.UUID,
default=None,
help="Filters the SyncSession models to those with matching 'client_instance_id'",
)
parser.add_argument(
"--server-instance-id",
type=uuid.UUID,
default=None,
help="Filters the SyncSession models to those with matching 'server_instance_id'",
)
parser.add_argument(
"--sync-filter",
type=str,
default=None,
help="Filters the TransferSession models to those with 'filters' starting with 'sync_filter'",
)
parser.add_argument(
"--push",
type=bool,
default=None,
help="Filters the TransferSession models to those with 'push' set to True",
)
parser.add_argument(
"--pull",
type=bool,
default=None,
help="Filters the TransferSession models to those with 'push' set to False",
)

def handle(self, *args, **options):

Expand All @@ -41,13 +72,28 @@ def handle(self, *args, **options):
if options["ids"]:
sync_sessions = sync_sessions.filter(id__in=options["ids"])

if options["client_instance_id"]:
sync_sessions = sync_sessions.filter(client_instance_id=options["client_instance_id"])

if options["server_instance_id"]:
sync_sessions = sync_sessions.filter(server_instance_id=options["server_instance_id"])

# retrieve all sessions still marked as active but with no activity since the cutoff
transfer_sessions = TransferSession.objects.filter(
sync_session_id__in=sync_sessions.values("id"),
active=True,
last_activity_timestamp__lt=cutoff,
)

if options["sync_filter"]:
transfer_sessions = transfer_sessions.filter(filter__startswith=options["sync_filter"])

if options["push"] == True and not options["pull"]:
transfer_sessions = transfer_sessions.filter(push=True)

if options["pull"] == True and not options["push"]:
transfer_sessions = transfer_sessions.filter(push=False)

transfer_count = transfer_sessions.count()

# loop over the stale sessions one by one to close them out
Expand Down
44 changes: 42 additions & 2 deletions tests/testapp/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from morango.models.core import TransferSession


def _create_sessions(last_activity_offset=0, sync_session=None):
def _create_sessions(last_activity_offset=0, sync_session=None, push=True):

last_activity_timestamp = timezone.now() - datetime.timedelta(
hours=last_activity_offset
Expand All @@ -21,13 +21,16 @@ def _create_sessions(last_activity_offset=0, sync_session=None):
id=uuid.uuid4().hex,
profile="facilitydata",
last_activity_timestamp=last_activity_timestamp,
client_instance_id=uuid.uuid4().hex,
server_instance_id=uuid.uuid4().hex,
)

transfer_session = TransferSession.objects.create(
id=uuid.uuid4().hex,
sync_session=sync_session,
push=True,
push=push,
last_activity_timestamp=last_activity_timestamp,
filter="1:2\n"
)

return sync_session, transfer_session
Expand Down Expand Up @@ -100,6 +103,43 @@ def test_filtering_sessions_cleared(self):
self.assertTransferSessionIsNotCleared(self.transfersession_new)
self.assertSyncSessionIsActive(self.syncsession_new)

def test_filtering_sessions_by_client_instance_id_cleared(self):
call_command("cleanupsyncs", client_instance_id=self.syncsession_old.client_instance_id, expiration=0)
self.assertTransferSessionIsCleared(self.transfersession_old)
self.assertSyncSessionIsNotActive(self.syncsession_old)
self.assertTransferSessionIsNotCleared(self.transfersession_new)
self.assertSyncSessionIsActive(self.syncsession_new)

def test_filtering_sessions_by_server_instance_id_cleared(self):
call_command("cleanupsyncs", server_instance_id=self.syncsession_old.server_instance_id, expiration=0)
self.assertTransferSessionIsCleared(self.transfersession_old)
self.assertSyncSessionIsNotActive(self.syncsession_old)
self.assertTransferSessionIsNotCleared(self.transfersession_new)
self.assertSyncSessionIsActive(self.syncsession_new)

def test_filtering_sessions_by_sync_filter_cleared(self):
call_command("cleanupsyncs", sync_filter=self.transfersession_old.filter, expiration=0)
self.assertTransferSessionIsCleared(self.transfersession_old)
self.assertSyncSessionIsNotActive(self.syncsession_old)
self.assertTransferSessionIsCleared(self.transfersession_new)
self.assertSyncSessionIsNotActive(self.syncsession_new)

def test_filtering_sessions_by_push_cleared(self):
call_command("cleanupsyncs", push=self.transfersession_old.push, expiration=0)
self.assertTransferSessionIsCleared(self.transfersession_old)
self.assertSyncSessionIsNotActive(self.syncsession_old)
self.assertTransferSessionIsCleared(self.transfersession_new)
self.assertSyncSessionIsNotActive(self.syncsession_new)

def test_filtering_sessions_by_pull_cleared(self):
syncsession_old, transfersession_old = _create_sessions(push=False)
syncsession_new, transfersession_new = _create_sessions(push=False)
call_command("cleanupsyncs", pull=not transfersession_old.push, expiration=0)
self.assertTransferSessionIsCleared(transfersession_old)
self.assertSyncSessionIsActive(syncsession_old)
self.assertTransferSessionIsCleared(transfersession_new)
self.assertSyncSessionIsActive(syncsession_new)

def test_multiple_ids_as_list(self):
ids = [self.syncsession_old.id, self.syncsession_new.id]
call_command("cleanupsyncs", ids=ids, expiration=0)
Expand Down

0 comments on commit 5ef2225

Please sign in to comment.