add strem like procedure and log progress (#13828)

This commit is contained in:
Chida82 2022-11-05 23:51:17 +01:00 committed by GitHub
parent 985f170383
commit bc6371aa83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -146,11 +146,22 @@ async def insert_from_delta_file(
async with aiohttp.ClientSession() as session:
async with session.get(server_info.url + "/" + filename, timeout=timeout) as resp:
resp.raise_for_status()
size = int(resp.headers.get("content-length", 0))
log.debug(f"Downloading delta file {filename}. Size {size} bytes.")
progress_byte = 0
progress_percentage = "{:.0%}".format(0)
target_filename = client_foldername.joinpath(filename)
text = await resp.read()
target_filename.write_bytes(text)
with target_filename.open(mode="wb") as f:
async for chunk, _ in resp.content.iter_chunks():
f.write(chunk)
progress_byte += len(chunk)
new_percentage = "{:.0%}".format(progress_byte / size)
if new_percentage != progress_percentage:
progress_percentage = new_percentage
log.info(f"Downloading delta file {filename}. {progress_percentage} of {size} bytes.")
except Exception:
target_filename = client_foldername.joinpath(filename)
os.remove(target_filename)
await data_store.server_misses_file(tree_id, server_info, timestamp)
raise