Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xmlupload): exit with code 0 if xmlupload finishes with batch (DEV-3396) #863

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from dsp_tools.commands.xmlupload.write_diagnostic_info import write_id2iri_mapping
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.exceptions import UserError
from dsp_tools.models.exceptions import XmlUploadInterruptedError
from dsp_tools.models.projectContext import ProjectContext
from dsp_tools.utils.connection import Connection
from dsp_tools.utils.connection_live import ConnectionLive
Expand Down Expand Up @@ -238,7 +239,7 @@ def upload_resources(
# The forseeable errors are already handled by failed_uploads
# Here we catch the unforseeable exceptions, incl. keyboard interrupt.
_handle_upload_error(
err_msg=str(err),
err=err,
iri_resolver=iri_resolver,
pending_resources=resources,
failed_uploads=failed_uploads,
Expand All @@ -264,7 +265,7 @@ def upload_resources(
# The forseeable errors are already handled by failed_uploads and nonapplied_stash.
# Here we catch the unforseeable exceptions, incl. keyboard interrupt.
_handle_upload_error(
err_msg=str(err),
err=err,
iri_resolver=iri_resolver,
pending_resources=resources,
failed_uploads=failed_uploads,
Expand Down Expand Up @@ -383,7 +384,7 @@ def _upload_resources(

Raises:
BaseException: in case of an unhandled exception during resource creation
KeyboardInterrupt: if the number of resources created is equal to the interrupt_after value
XmlUploadInterruptedError: if the number of resources created is equal to the interrupt_after value

Returns:
id2iri_mapping, failed_uploads
Expand All @@ -408,7 +409,7 @@ def _upload_resources(

for i, resource in enumerate(resources.copy()):
if i >= interrupt_after:
raise KeyboardInterrupt(f"Interrupted: Maximum number of resources was reached ({interrupt_after})")
raise XmlUploadInterruptedError(f"Interrupted: Maximum number of resources was reached ({interrupt_after})")
success, media_info = handle_media_info(
resource, config.media_previously_uploaded, sipi_server, imgdir, permissions_lookup
)
Expand Down Expand Up @@ -478,7 +479,7 @@ def _create_resource(


def _handle_upload_error(
err_msg: str,
err: BaseException,
iri_resolver: IriResolver,
pending_resources: list[XMLResource],
failed_uploads: list[str],
Expand All @@ -497,34 +498,39 @@ def _handle_upload_error(
It then quits the Python interpreter with exit code 1.

Args:
err_msg: string representation of the error that was the cause of the abort
err: the error that was the cause of the abort
iri_resolver: a resolver for internal IDs to IRIs
pending_resources: resources that were not uploaded to DSP
failed_uploads: resources that caused an error when uploading to DSP
pending_stash: an object that contains all stashed links that could not yet be reapplied to their resources
config: the upload configuration
permissions_lookup: dictionary that contains the permission name as string and the corresponding Python object
"""
logfiles = ", ".join([handler.baseFilename for handler in logger.handlers if isinstance(handler, FileHandler)])
msg = (
f"\n==========================================\n"
f"{datetime.now()}: xmlupload must be aborted because of an error.\n"
f"Error message: '{err_msg}'\n"
f"For more information, see the log file: {logfiles}\n"
)
if isinstance(err, XmlUploadInterruptedError):
msg = "\n==========================================\n" + err.message + "\n"
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved
exit_code = 0
else:
logfiles = ", ".join([handler.baseFilename for handler in logger.handlers if isinstance(handler, FileHandler)])
msg = (
f"\n==========================================\n"
f"{datetime.now()}: xmlupload must be aborted because of an error.\n"
f"Error message: '{err}'\n"
f"For more information, see the log file: {logfiles}\n"
)
exit_code = 1

upload_state = UploadState(
pending_resources, failed_uploads, iri_resolver.lookup, pending_stash, config, permissions_lookup
)
msg += _save_upload_state(upload_state)

if failed_uploads:
msg += f"Independently of this error, there were some resources that could not be uploaded: {failed_uploads}\n"
msg += f"Independently from this, there were some resources that could not be uploaded: {failed_uploads}\n"

logger.exception(msg)
print(msg)

sys.exit(1)
sys.exit(exit_code)


def _save_upload_state(upload_state: UploadState) -> str:
Expand Down
4 changes: 4 additions & 0 deletions src/dsp_tools/models/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ class BadCredentialsError(PermanentConnectionError):

class XmlUploadError(BaseError):
"""Represents an error raised in the context of the xmlupload."""


class XmlUploadInterruptedError(XmlUploadError):
"""Represents an error raised when the xmlupload was interrupted."""