Skip to content

Commit

Permalink
Merge pull request #194 from bjester/optional-ignore-on-resume
Browse files Browse the repository at this point in the history
Add optional setting to ignore an existing process on resuming a sync
  • Loading branch information
bjester committed Sep 28, 2023
2 parents 3eb9b13 + 6e9a653 commit 4e51901
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
List of the most important changes for each release.
## 0.6.17
- Added `client-instance-id`, `server-instance-id`, `sync-filter`, `push` and `pull` arguments to `cleanupsyncs` management command
- Added option for resuming a sync to ignore the `SyncSession`'s existing `process_id`
- Added custom user agent to sync HTTP requests
- Fixed documentation build issues
- Makefile no longer defines shell with explicit path

## 0.6.16
- Added dedicated `client_instance_id` and `server_instance_id` fields to `SyncSession`
Expand Down
9 changes: 7 additions & 2 deletions morango/sync/syncsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,16 @@ def create_sync_session(self, client_cert, server_cert, chunk_size=None):
sync_session = SyncSession.objects.create(**data)
return SyncSessionClient(self, sync_session)

def resume_sync_session(self, sync_session_id, chunk_size=None):
def resume_sync_session(self, sync_session_id, chunk_size=None, ignore_existing_process=False):
"""
Resumes an existing sync session given an ID
:param sync_session_id: The UUID of the `SyncSession` to resume
:param chunk_size: An optional parameter specifying the size for each transferred chunk
:type chunk_size: int
:param ignore_existing_process:An optional parameter specifying whether to ignore an
existing active process ID
:type ignore_existing_process: bool
:return: A SyncSessionClient instance
:rtype: SyncSessionClient
"""
Expand All @@ -281,7 +285,8 @@ def resume_sync_session(self, sync_session_id, chunk_size=None):

# check that process of existing session isn't still running
if (
sync_session.process_id
not ignore_existing_process
and sync_session.process_id
and sync_session.process_id != os.getpid()
and pid_exists(sync_session.process_id)
):
Expand Down
24 changes: 24 additions & 0 deletions tests/testapp/tests/sync/test_syncsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,30 @@ def create(**data):
with self.assertRaises(MorangoResumeSyncError):
self.network_connection.resume_sync_session(sync_session.id)

@mock.patch.object(SyncSession.objects, "create")
def test_resume_sync_session__still_running_but_ignore(self, mock_create):
def create(**data):
"""Trickery to get around same DB being used for both client and server"""
return SyncSession.objects.get(pk=data.get("id"))

mock_create.side_effect = create

# first create a session
client = self.network_connection.create_sync_session(self.subset_cert, self.root_cert)
# reset process ID
sync_session = client.sync_session
sync_session.process_id = 123000111
sync_session.save()

with mock.patch("morango.sync.syncsession.pid_exists") as mock_pid_exists:
mock_pid_exists.return_value = True
with mock.patch("morango.sync.syncsession.os.getpid") as mock_getpid:
mock_getpid.return_value = 245111222
resume_client = self.network_connection.resume_sync_session(
sync_session.id, ignore_existing_process=True
)
self.assertEqual(sync_session.id, resume_client.sync_session.id)


class SyncSessionClientTestCase(BaseClientTestCase):
def test_get_pull_client(self):
Expand Down

0 comments on commit 4e51901

Please sign in to comment.