Skip to content

Commit

Permalink
fix(resume-xmlupload): make resource counting more user friendly (DEV…
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Mar 8, 2024
1 parent 8a15973 commit 2b5295d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/dsp_tools/commands/resume_xmlupload/resume_xmlupload.py
Expand Up @@ -11,6 +11,9 @@
from dsp_tools.commands.xmlupload.xmlupload import cleanup_upload
from dsp_tools.commands.xmlupload.xmlupload import upload_resources
from dsp_tools.utils.connection_live import ConnectionLive
from dsp_tools.utils.create_logger import get_logger

logger = get_logger(__name__)


def resume_xmlupload(
Expand All @@ -33,6 +36,17 @@ def resume_xmlupload(
uploaded because there is an error in it
"""
upload_state = _read_upload_state_from_disk(server)
previous_successful = len(upload_state.iri_resolver_lookup)
previous_failed = len(upload_state.failed_uploads)
previous_total = previous_successful + previous_failed
msg = (
f"Resuming upload for project {upload_state.config.shortcode} on server {server}. "
f"Number of resources uploaded until now: {previous_total}"
)
if previous_failed:
msg += f" ({previous_failed} of them failed)"
logger.info(msg)
print("\n==========================\n" + msg + "\n==========================\n")

con = ConnectionLive(server)
con.login(user, password)
Expand Down
15 changes: 11 additions & 4 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Expand Up @@ -403,11 +403,15 @@ def _upload_resources(
media_previously_ingested=config.media_previously_uploaded,
)

total_res = len(resources)
total_res = len(resources) + len(iri_resolver.lookup)
previous_successful = len(iri_resolver.lookup)
previous_failed = len(failed_uploads)
previous_total = previous_successful + previous_failed
# if the interrupt_after value is not set, the upload will not be interrupted
interrupt_after = config.interrupt_after or total_res + 1

for i, resource in enumerate(resources.copy()):
current_res = i + 1 + previous_total
if i >= interrupt_after:
raise XmlUploadInterruptedError(f"Interrupted: Maximum number of resources was reached ({interrupt_after})")
success, media_info = handle_media_info(
Expand All @@ -427,12 +431,12 @@ def _upload_resources(
else:
# resource creation succeeded: update the iri_resolver and remove the resource from the list
iri, label = res
_tidy_up_resource_creation(iri, label, iri_resolver, resource, i + 1, total_res) # type: ignore[arg-type]
_tidy_up_resource_creation(iri, label, iri_resolver, resource, current_res, total_res) # type: ignore[arg-type]
except BaseException as err:
if res and res[0]:
# creation succeeded, but during tidy up, a Keyboard Interrupt occurred. tidy up again before escalating
iri, label = res
_tidy_up_resource_creation(iri, label, iri_resolver, resource, i + 1, total_res)
_tidy_up_resource_creation(iri, label, iri_resolver, resource, current_res, total_res)
else:
# unhandled exception during resource creation
failed_uploads.append(resource.res_id)
Expand Down Expand Up @@ -527,7 +531,10 @@ def _handle_upload_error(
if failed_uploads:
msg += f"Independently from this, there were some resources that could not be uploaded: {failed_uploads}\n"

logger.exception(msg)
if exit_code == 1:
logger.exception(msg)
else:
logger.info(msg)
print(msg)

sys.exit(exit_code)
Expand Down

0 comments on commit 2b5295d

Please sign in to comment.