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

[do not merge]: copy LHTT files to same external disk #1372

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions src/dsp_tools/commands/ingest_xmlupload/bulk_ingest_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import urllib.parse
from dataclasses import dataclass
from dataclasses import field
Expand All @@ -7,12 +8,12 @@
import regex
from loguru import logger
from requests import JSONDecodeError
from requests import RequestException

Check failure on line 11 in src/dsp_tools/commands/ingest_xmlupload/bulk_ingest_client.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F401)

src/dsp_tools/commands/ingest_xmlupload/bulk_ingest_client.py:11:22: F401 `requests.RequestException` imported but unused
from requests import Session
from requests.adapters import HTTPAdapter
from requests.adapters import Retry

from dsp_tools.commands.ingest_xmlupload.upload_files.upload_failures import UploadFailure

Check failure on line 16 in src/dsp_tools/commands/ingest_xmlupload/bulk_ingest_client.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F401)

src/dsp_tools/commands/ingest_xmlupload/bulk_ingest_client.py:16:78: F401 `dsp_tools.commands.ingest_xmlupload.upload_files.upload_failures.UploadFailure` imported but unused
from dsp_tools.models.exceptions import BadCredentialsError
from dsp_tools.models.exceptions import UserError
from dsp_tools.utils.authentication_client import AuthenticationClient
Expand Down Expand Up @@ -57,39 +58,19 @@
def upload_file(
self,
filepath: Path,
) -> UploadFailure | None:
) -> None:
"""
Uploads a file to the ingest server.
The load balancer on DSP servers currently has a timeout of 60s, so we need to use a timeout of 58s.
See https://github.com/dasch-swiss/dsp-tools/pull/1335/files#r1882508057
# noqa: DAR101
# noqa: DAR201
"""
timeout = 58
url = self._build_url_for_bulk_ingest_ingest_route(filepath)
headers = {"Content-Type": "application/octet-stream"}
err_msg = f"Failed to upload '{filepath}' to '{url}'."
try:
logger.debug(f"REQUEST: POST to {url}, timeout: {timeout}, headers: {headers}")
with open(self.imgdir / filepath, "rb") as binary_io:
res = self.session.post(
url=url,
headers=headers,
data=binary_io, # https://requests.readthedocs.io/en/latest/user/advanced/#streaming-uploads
timeout=timeout,
)
logger.debug(f"RESPONSE: {res.status_code}")
except RequestException as e:
logger.error(err_msg)
return UploadFailure(filepath, f"Exception of requests library: {e}")
except OSError as e:
err_msg = f"Cannot bulk-ingest {filepath}, because the file could not be opened/read: {e.strerror}"
logger.error(err_msg)
return UploadFailure(filepath, err_msg)
if res.status_code != STATUS_OK:
logger.error(err_msg)
return UploadFailure(filepath, res.reason, res.status_code, res.text)
return None
filepath_rel = filepath.relative_to("/") if filepath.is_absolute() else filepath
target_base = Path("/Volumes/Thoth/dsp-api/sipi/tmp-dsp-ingest/import/0820")
target = target_base / filepath_rel
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(filepath, target)

def _build_url_for_bulk_ingest_ingest_route(self, filepath: Path) -> str:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def upload_files(
failures: list[UploadFailure] = []
progress_bar = tqdm(paths, desc="Uploading files", unit="file(s)", dynamic_ncols=True)
for path in progress_bar:
if res := ingest_client.upload_file(path):
failures.append(res)
progress_bar.set_description(f"Uploading files (failed: {len(failures)})")
ingest_client.upload_file(path)
if failures:
aggregated_failures = UploadFailures(failures, len(paths), shortcode, creds.dsp_ingest_url)
msg = aggregated_failures.execute_error_protocol()
Expand Down
Loading