From afd948576d76b124072191618b3f8edafcff138e Mon Sep 17 00:00:00 2001 From: Christopher Usher Date: Fri, 28 Dec 2018 23:47:26 -0800 Subject: [PATCH] Forgot to try to remove temporary file --- backfiller/backfiller/main.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/backfiller/backfiller/main.py b/backfiller/backfiller/main.py index e2fedfc..9f09e0a 100644 --- a/backfiller/backfiller/main.py +++ b/backfiller/backfiller/main.py @@ -72,7 +72,7 @@ def list_remote_segments(node, stream, variant, hour, timeout=TIMEOUT): def get_remote_segment(base_dir, node, stream, variant, hour, missing_segment, timeout=TIMEOUT): - + file_created = False path = os.path.join(base_dir, stream, variant, hour, missing_segment) if os.path.exists(path): return @@ -81,12 +81,23 @@ def get_remote_segment(base_dir, node, stream, variant, hour, missing_segment, temp_path = '-'.join(substrs[:-1] + [str(uuid.uuid4()) + '.st']) common.ensure_directory(temp_path) - uri = '{}/segments/{}/{}/{}/{}'.format(node, stream, variant, hour, missing_segment) - resp = requests.get(uri, stream=True, timeout=timeout) + try: + uri = '{}/segments/{}/{}/{}/{}'.format(node, stream, variant, hour, missing_segment) + resp = requests.get(uri, stream=True, timeout=timeout) + + with open(temp_path, 'w') as f: + file_created = True + for chunk in resp.iter_content(8192): + f.write(chunk) - with open(temp_path, 'w') as f: - for chunk in resp.iter_content(8192): - f.write(chunk) + except Exception: + ex_type, ex, tb = sys.exc_info() + if file_created: + os.remove(temp_path) + + raise ex_type, ex, tb + + common.rename(temp_path, path)