Skip to content

Commit

Permalink
ensure auto-close of response using context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Nov 27, 2024
1 parent adb9560 commit 87e6ebf
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions system/loggerd/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,36 +171,34 @@ def upload(self, name: str, key: str, fn: str, network_type: int, metered: bool)

cloudlog.event("upload_start", key=key, fn=fn, sz=sz, network_type=network_type, metered=metered)

success = False
if sz == 0:
# tag files of 0 size as uploaded
success = True
elif name in MAX_UPLOAD_SIZES and sz > MAX_UPLOAD_SIZES[name]:
cloudlog.event("uploader_too_large", key=key, fn=fn, sz=sz)
success = True
else:
start_time = time.monotonic()

stat = None
last_exc = None
try:
stat = self.do_upload(key, fn)
start_time = time.monotonic()
with self.do_upload(key, fn) as stat:
if stat is not None and stat.status_code in (200, 201, 401, 403, 412):
self.last_filename = fn
if stat.status_code == 412:
cloudlog.event("upload_ignored", key=key, fn=fn, sz=sz, network_type=network_type, metered=metered)
else:
dt = time.monotonic() - start_time
content_length = int(stat.request.headers.get("Content-Length", 0))
speed = (content_length / 1e6) / dt
cloudlog.event("upload_success", key=key, fn=fn, sz=sz, content_length=content_length,
network_type=network_type, metered=metered, speed=speed)
success = True
else:
cloudlog.event("upload_failed", stat=stat, key=key, fn=fn, sz=sz, network_type=network_type, metered=metered)

except Exception as e:
last_exc = (e, traceback.format_exc())

if stat is not None and stat.status_code in (200, 201, 401, 403, 412):
self.last_filename = fn
dt = time.monotonic() - start_time
if stat.status_code == 412:
cloudlog.event("upload_ignored", key=key, fn=fn, sz=sz, network_type=network_type, metered=metered)
else:
content_length = int(stat.request.headers.get("Content-Length", 0))
speed = (content_length / 1e6) / dt
cloudlog.event("upload_success", key=key, fn=fn, sz=sz, content_length=content_length,
network_type=network_type, metered=metered, speed=speed)
success = True
else:
success = False
cloudlog.event("upload_failed", stat=stat, exc=last_exc, key=key, fn=fn, sz=sz, network_type=network_type, metered=metered)
cloudlog.event("upload_exception", exc=last_exc, key=key, fn=fn, sz=sz, network_type=network_type, metered=metered)

if success:
# tag file as uploaded
Expand Down

0 comments on commit 87e6ebf

Please sign in to comment.