Skip to content

Commit 0128a06

Browse files
committed
Address comstic comments from @varmar05 and some from @wonder-sk
+ black staff
1 parent 42c34a2 commit 0128a06

File tree

8 files changed

+159
-220
lines changed

8 files changed

+159
-220
lines changed

mergin/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,13 @@ def sync(ctx):
477477
return
478478
directory = os.getcwd()
479479
upload_job = None
480-
length = 1
481480
try:
482481
def on_progress(increment, push_job):
483482
nonlocal upload_job
484483
upload_job = push_job
485484

486485
# run pull & push cycles until there are no local changes
487-
mc.sync_project_with_callback(directory, progress_callback=on_progress)
486+
mc.sync_project(directory, progress_callback=on_progress)
488487

489488
click.secho("Sync complete.", fg="green")
490489

mergin/client.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
from typing import List
2222

2323
from .common import (
24+
PUSH_ATTEMPT_WAIT,
25+
PUSH_ATTEMPTS,
26+
SYNC_CALLBACK_WAIT,
2427
ClientError,
2528
ErrorCode,
2629
LoginError,
@@ -916,7 +919,7 @@ def push_project(self, directory):
916919
:param directory: Project's directory
917920
:type directory: String
918921
"""
919-
job = push_project_async(self, directory)
922+
job = push_project_async(self, directory, check_version=True)
920923
if job is None:
921924
return # there is nothing to push (or we only deleted some files)
922925
push_project_wait(job)
@@ -1494,7 +1497,7 @@ def create_invitation(self, workspace_id: int, email: str, workspace_role: Works
14941497
ws_inv = self.post(f"v2/workspaces/{workspace_id}/invitations", params, json_headers)
14951498
return json.load(ws_inv)
14961499

1497-
def sync_project(self, project_directory):
1500+
def sync_project(self, project_directory, progress_callback=None):
14981501
"""
14991502
Syncs project by loop with these steps:
15001503
1. Pull server version
@@ -1515,61 +1518,24 @@ def sync_project(self, project_directory):
15151518
job = push_project_async(self, project_directory)
15161519
if not job:
15171520
break
1518-
push_project_wait(job)
1519-
push_project_finalize(job)
1520-
_, has_changes = get_push_changes_batch(self, mp, job.server_resp)
1521-
except ClientError as e:
1522-
if (
1523-
e.sync_retry
1524-
and server_conflict_attempts < 2
1525-
):
1526-
# retry on conflict, e.g. when server has changes that we do not have yet
1527-
mp.log.info(
1528-
"Attempting sync process due to conflicts between server and local directory or another user is syncing."
1529-
)
1530-
server_conflict_attempts += 1
1531-
sleep(5)
1532-
continue
1533-
raise e
1534-
1535-
def sync_project_with_callback(self, project_directory, progress_callback=None, sleep_time=0.1):
1536-
"""
1537-
Syncs project while sending push progress info as callback.
1538-
Sync is done in this loop:
1539-
Pending changes? -> Pull -> Get changes batch -> Push the changes -> repeat
1540-
:param progress_callback: updates the progress bar in CLI, on_progress(increment)
1541-
:param sleep_time: sleep time between calling the callback function
1542-
"""
1543-
mp = MerginProject(project_directory)
1544-
has_changes = True
1545-
server_conflict_attempts = 0
1546-
while has_changes:
1547-
pull_job = pull_project_async(self, project_directory)
1548-
if pull_job:
1549-
pull_project_wait(pull_job)
1550-
pull_project_finalize(pull_job)
1551-
try:
1552-
job = push_project_async(self, project_directory)
1553-
if not job:
1554-
break
1555-
last = 0
1556-
while push_project_is_running(job):
1557-
sleep(sleep_time)
1558-
now = job.transferred_size
1559-
progress_callback(now - last, job) # update progressbar with transferred size increment
1560-
last = now
1521+
if not progress_callback:
1522+
push_project_wait(job)
1523+
else:
1524+
last_size = 0
1525+
while push_project_is_running(job):
1526+
sleep(SYNC_CALLBACK_WAIT)
1527+
current_size = job.transferred_size
1528+
progress_callback(current_size - last_size, job) # call callback with transferred size increment
1529+
last = current_size
15611530
push_project_finalize(job)
15621531
_, has_changes = get_push_changes_batch(self, mp, job.server_resp)
15631532
except ClientError as e:
1564-
if (
1565-
e.sync_retry
1566-
and server_conflict_attempts < 2
1567-
):
1533+
if e.sync_retry and server_conflict_attempts <= PUSH_ATTEMPTS:
15681534
# retry on conflict, e.g. when server has changes that we do not have yet
15691535
mp.log.info(
1570-
"Attempting sync process due to conflicts between server and local directory or another user is syncing."
1536+
f"Restarting sync process (conflict on server) - {server_conflict_attempts + 1}/{PUSH_ATTEMPTS}"
15711537
)
15721538
server_conflict_attempts += 1
1573-
sleep(5)
1539+
sleep(PUSH_ATTEMPT_WAIT)
15741540
continue
15751541
raise e

0 commit comments

Comments
 (0)