Skip to content

Commit 1394c94

Browse files
committed
Some refactor on loggin changes
1 parent c0ac231 commit 1394c94

File tree

3 files changed

+67
-50
lines changed

3 files changed

+67
-50
lines changed

mergin/client_push.py

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ def dump(self):
165165
print(f"- {item.file_path} {item.chunk_index} {item.size}")
166166
print("--- END ---")
167167

168+
def log_upload_details_error(self, err: ClientError):
169+
"""Log details about the upload job changes accumulated with ClientError"""
170+
http_code = getattr(err, "http_error", None)
171+
self.mp.log.error(
172+
f"Push failed with HTTP code {http_code}. "
173+
f"Upload details: {len(self.upload_queue_items)} file chunks, total size {self.total_size} bytes."
174+
)
175+
self.mp.log.error("Files:")
176+
for f in self.changes.get_upload_changes():
177+
path = f.path if f.path else "<unknown>"
178+
size = f.size if f.size else "?"
179+
diff_info = f.get_diff()
180+
if diff_info:
181+
diff_size = diff_info.size if diff_info.size else "?"
182+
# best-effort: number of geodiff changes (if available)
183+
changes_cnt = self.mp.get_geodiff_changes_count(diff_info.path)
184+
if changes_cnt is None:
185+
self.mp.log.error(f" - {path}, size={size}, diff_size={diff_size}, changes=n/a")
186+
else:
187+
self.mp.log.error(f" - {path}, size={size}, diff_size={diff_size}, changes={changes_cnt}")
188+
else:
189+
self.mp.log.error(f" - {path}, size={size}")
190+
168191
def _submit_item_to_thread(self, item: UploadQueueItem):
169192
"""Upload a single item in the background"""
170193
future = self.executor.submit(_do_upload, item, self)
@@ -404,6 +427,10 @@ def push_project_finalize(job: UploadJob):
404427
if not err.is_retryable_sync():
405428
# clear the upload chunks cache, as we are getting fatal from server
406429
job.mc.upload_chunks_cache.clear()
430+
http_code = getattr(err, "http_error", None)
431+
if http_code in (502, 504):
432+
job.log_upload_details_error(err)
433+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
407434
job.mp.log.error("--- push finish failed! " + str(err))
408435
raise err
409436
elif with_upload_of_files:
@@ -415,25 +442,7 @@ def push_project_finalize(job: UploadJob):
415442
# Log additional metadata on server error 502 or 504 (extended logging only)
416443
http_code = getattr(err, "http_error", None)
417444
if http_code in (502, 504):
418-
job.mp.log.error(
419-
f"Push failed with HTTP error {http_code}. "
420-
f"Upload details: {len(job.upload_queue_items)} file chunks, total size {job.total_size} bytes."
421-
)
422-
job.mp.log.error("Files:")
423-
for f in job.changes.get("added", []) + job.changes.get("updated", []):
424-
path = f.get("path", "<unknown>")
425-
size = f.get("size", "?")
426-
if "diff" in f:
427-
diff_info = f.get("diff", {})
428-
diff_size = diff_info.get("size", "?")
429-
# best-effort: number of geodiff changes (if available)
430-
changes_cnt = _geodiff_changes_count(job.mp, diff_info.get("path"))
431-
if changes_cnt is None:
432-
job.mp.log.error(f" - {path}, size={size}, diff_size={diff_size}, changes=n/a")
433-
else:
434-
job.mp.log.error(f" - {path}, size={size}, diff_size={diff_size}, changes={changes_cnt}")
435-
else:
436-
job.mp.log.error(f" - {path}, size={size}")
445+
job.log_upload_details_error(err)
437446

438447
# server returns various error messages with filename or something generic
439448
# it would be better if it returned list of failed files (and reasons) whenever possible
@@ -466,25 +475,6 @@ def push_project_finalize(job: UploadJob):
466475
job.mp.log.info("--- push finished - new project version " + job.server_resp["version"])
467476

468477

469-
def _geodiff_changes_count(mp: MerginProject, diff_rel_path: str):
470-
"""
471-
Best-effort: return number of changes in the .gpkg diff (int) or None.
472-
Never raises – diagnostics/logging must not fail.
473-
"""
474-
475-
diff_abs = mp.fpath_meta(diff_rel_path)
476-
try:
477-
return pygeodiff.GeoDiff().changes_count(diff_abs)
478-
except (
479-
pygeodiff.GeoDiffLibError,
480-
pygeodiff.GeoDiffLibConflictError,
481-
pygeodiff.GeoDiffLibUnsupportedChangeError,
482-
pygeodiff.GeoDiffLibVersionError,
483-
FileNotFoundError,
484-
):
485-
return None
486-
487-
488478
def push_project_cancel(job: UploadJob):
489479
"""
490480
To be called (from main thread) to cancel a job that has uploads in progress.
@@ -499,6 +489,7 @@ def push_project_cancel(job: UploadJob):
499489
if not job.transaction_id:
500490
# If not v2 api endpoint with transaction, nothing to cancel on server
501491
job.mp.log.info("--- push cancelled")
492+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
502493
return
503494
try:
504495
resp_cancel = job.mc.post("/v1/project/push/cancel/%s" % job.transaction_id)

mergin/merginproject.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,3 +857,21 @@ def set_tables_to_skip(self, tables):
857857
:type tables: list[str]
858858
"""
859859
self.geodiff.set_tables_to_skip(tables)
860+
861+
def get_geodiff_changes_count(self, diff_rel_path: str):
862+
"""
863+
Best-effort: return number of changes in the .gpkg diff (int) or None.
864+
Never raises – diagnostics/logging must not fail.
865+
"""
866+
867+
diff_abs = self.fpath_meta(diff_rel_path)
868+
try:
869+
return pygeodiff.GeoDiff().changes_count(diff_abs)
870+
except (
871+
pygeodiff.GeoDiffLibError,
872+
pygeodiff.GeoDiffLibConflictError,
873+
pygeodiff.GeoDiffLibUnsupportedChangeError,
874+
pygeodiff.GeoDiffLibVersionError,
875+
FileNotFoundError,
876+
):
877+
return None

mergin/test/test_push_logging.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from types import SimpleNamespace
23
from pathlib import Path
34
import logging
@@ -8,6 +9,7 @@
89
from pygeodiff import GeoDiff
910
from mergin.client_push import push_project_finalize, UploadJob
1011
from mergin.common import ClientError
12+
from mergin.local_changes import FileChange, FileChange, LocalProjectChanges
1113
from mergin.merginproject import MerginProject
1214
from mergin.client import MerginClient
1315

@@ -59,19 +61,25 @@ def mc_post(url, *args, **kwargs):
5961

6062
# build a real UploadJob that references the diff/file sizes
6163
job = UploadJob(
62-
project_path="u/p",
63-
changes={
64-
"added": [],
65-
"updated": [
66-
{
67-
"path": modified.name,
68-
"size": file_size,
69-
"diff": {"path": diff_rel, "size": diff_size},
70-
"chunks": [1],
71-
}
64+
version="v1",
65+
changes=LocalProjectChanges(
66+
added=[],
67+
updated=[
68+
FileChange(
69+
path=modified.name,
70+
size=file_size,
71+
diff={"path": diff_rel, "size": diff_size},
72+
chunks=["123"],
73+
checksum="abc",
74+
mtime=datetime.datetime.now(),
75+
upload_file=None,
76+
version=None,
77+
history=None,
78+
location=None,
79+
)
7280
],
73-
"removed": [],
74-
},
81+
removed=[],
82+
),
7583
transaction_id=tx,
7684
mp=mp,
7785
mc=mc,

0 commit comments

Comments
 (0)