From bc119aed226fa22997c96c68823f98ad2eef7b08 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 5 Feb 2015 15:00:19 -0500 Subject: [PATCH] Clarify why we need database.UserThenDisconnect --- endpoints/verbs.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/endpoints/verbs.py b/endpoints/verbs.py index 3999d64e7..091af8841 100644 --- a/endpoints/verbs.py +++ b/endpoints/verbs.py @@ -61,16 +61,19 @@ def _sign_sythentic_image(verb, linked_storage_uuid, queue_file): logger.exception('Exception when signing %s image %s', verb, linked_storage_uuid) return - with database.UseThenDisconnect(app.config): - try: - derived = model.get_storage_by_uuid(linked_storage_uuid) - except model.InvalidImageException: - return + # Setup the database (since this is a new process) and then disconnect immediately + # once the operation completes. + if not queue_file.raised_exception: + with database.UseThenDisconnect(app.config): + try: + derived = model.get_storage_by_uuid(linked_storage_uuid) + except model.InvalidImageException: + return - signature_entry = model.find_or_create_storage_signature(derived, signer.name) - signature_entry.signature = signature - signature_entry.uploading = False - signature_entry.save() + signature_entry = model.find_or_create_storage_signature(derived, signer.name) + signature_entry.signature = signature + signature_entry.uploading = False + signature_entry.save() def _write_synthetic_image_to_storage(verb, linked_storage_uuid, linked_locations, queue_file): @@ -89,6 +92,8 @@ def _write_synthetic_image_to_storage(verb, linked_storage_uuid, linked_location queue_file.close() if not queue_file.raised_exception: + # Setup the database (since this is a new process) and then disconnect immediately + # once the operation completes. with database.UseThenDisconnect(app.config): done_uploading = model.get_storage_by_uuid(linked_storage_uuid) done_uploading.uploading = False