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

chore(xmlupload): improve error handling of Timeout Error (DEV-2299) #411

Merged
merged 6 commits into from
Jun 20, 2023
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
19 changes: 13 additions & 6 deletions src/dsp_tools/utils/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pandas as pd
import regex
from lxml import etree
from requests import RequestException
from requests import ReadTimeout, RequestException

from dsp_tools.models.connection import Connection
from dsp_tools.models.exceptions import BaseError, UserError
Expand Down Expand Up @@ -75,7 +75,7 @@ def try_network_action(
Returns:
the return value of action
"""

action_as_str = f"action='{action}', args='{args}', kwargs='{kwargs}'"
for i in range(7):
try:
if args and not kwargs:
Expand All @@ -86,9 +86,16 @@ def try_network_action(
return action(*args, **kwargs)
else:
return action()
except (TimeoutError, ReadTimeout):
msg = f"Timeout Error: Try reconnecting to DSP server, next attempt in {2 ** i} seconds..."
print(f"{datetime.now().isoformat()}: {msg}")
logger.error(f"{msg} {action_as_str} (retry-counter i={i})", exc_info=True)
time.sleep(2**i)
continue
except (ConnectionError, RequestException):
print(f"{datetime.now().isoformat()}: Try reconnecting to DSP server, next attempt in {2 ** i} seconds...")
logger.error(f"Try reconnecting to DSP server, next attempt in {2 ** i} seconds...", exc_info=True)
msg = f"Network Error: Try reconnecting to DSP server, next attempt in {2 ** i} seconds..."
print(f"{datetime.now().isoformat()}: {msg}")
logger.error(f"{msg} {action_as_str} (retry-counter i={i})", exc_info=True)
time.sleep(2**i)
continue
except BaseError as err:
Expand All @@ -97,9 +104,9 @@ def try_network_action(
in_500_range = 500 <= err.status_code < 600
try_again_later = "try again later" in err.message
if try_again_later or in_500_range:
msg = f"Try reconnecting to DSP server, next attempt in {2 ** i} seconds..."
msg = f"Transient Error: Try reconnecting to DSP server, next attempt in {2 ** i} seconds..."
print(f"{datetime.now().isoformat()}: {msg}")
logger.error(msg, exc_info=True)
logger.error(f"{msg} {action_as_str} (retry-counter i={i})", exc_info=True)
time.sleep(2**i)
continue
else:
Expand Down
24 changes: 11 additions & 13 deletions src/dsp_tools/utils/xml_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ def _handle_upload_error(
that all information about what is already in DSP
is written into diagnostic files.

It then re-raises the original error.
It then quits the Python interpreter with exit code 1.

Args:
err: error that was the cause of the abort
Expand All @@ -1041,9 +1041,12 @@ def _handle_upload_error(
Returns:
None
"""

print("\n==========================================\nxmlupload must be aborted because of an error")
logger.info("xmlupload must be aborted because of an error")
print(
f"\n==========================================\n"
f"xmlupload must be aborted because of an error.\n"
f"Error message: '{err}'\n"
)
logger.error("xmlupload must be aborted because of an error", exc_info=err)
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved

# only stashed properties of resources that already exist in DSP are of interest
stashed_xml_texts = _purge_stashed_xml_texts(
Expand All @@ -1056,7 +1059,7 @@ def _handle_upload_error(
)

if id2iri_mapping:
id2iri_mapping_file = f"{save_location}/{timestamp_str}_id2iri_mapping.json"
id2iri_mapping_file = f"{save_location}/{timestamp_str}_id2iri_mapping.json\n"
with open(id2iri_mapping_file, "x", encoding="utf-8") as f:
json.dump(id2iri_mapping, f, ensure_ascii=False, indent=4)
print(f"The mapping of internal IDs to IRIs was written to {id2iri_mapping_file}")
Expand All @@ -1077,7 +1080,7 @@ def _handle_upload_error(
)
msg = (
f"There are stashed text properties that could not be reapplied to the resources they were stripped from. "
f"They were saved to {xml_filename}."
f"They were saved to {xml_filename}.\n"
)
print(msg)
logger.info(msg)
Expand All @@ -1096,7 +1099,7 @@ def _handle_upload_error(
)
msg = (
f"There are stashed resptr properties that could not be reapplied "
f"to the resources they were stripped from. They were saved to {resptr_filename}"
f"to the resources they were stripped from. They were saved to {resptr_filename}\n"
)
print(msg)
logger.info(msg)
Expand All @@ -1107,9 +1110,4 @@ def _handle_upload_error(
print(msg)
logger.info(msg)

if isinstance(err, KeyboardInterrupt):
sys.exit(1)
else:
print("The error will now be raised again:\n==========================================\n")
logger.info("The error will now be raised again")
raise err
sys.exit(1)
2 changes: 0 additions & 2 deletions testdata/xml-data/test-data-systematic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,6 @@
</text-prop>
</resource>

<!-- the API supports also MP4 files that are purely audio (MIME "audio/mp4), but there is no test file for it -->

<resource label="Moviething"
restype=":MovieThing"
id="video_thing_1"
Expand Down