From 1beb4dafa16d48ce6fae68da707835da9a93474d Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 5 Jan 2024 12:34:30 -0500 Subject: [PATCH] fix: Fix delete_step to only run finish() if error isn't unexpected --- process/management/commands/file_worker.py | 2 +- process/util.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/process/management/commands/file_worker.py b/process/management/commands/file_worker.py index 1347a366..02685f4e 100644 --- a/process/management/commands/file_worker.py +++ b/process/management/commands/file_worker.py @@ -65,9 +65,9 @@ def callback(client_state, channel, method, properties, input_message): try: with delete_step( ProcessingStep.Name.LOAD, + collection_file_id=collection_file_id, finish=finish, finish_args=(collection_id, collection_file_id), - collection_file_id=collection_file_id, ): with transaction.atomic(): collection_file = CollectionFile.objects.select_related("collection").get(pk=collection_file_id) diff --git a/process/util.py b/process/util.py index 457ae05e..2c874499 100644 --- a/process/util.py +++ b/process/util.py @@ -127,22 +127,19 @@ def create_step(name, collection_id, **kwargs): @contextmanager -def delete_step(*args, finish=None, finish_args=(), **kwargs): +def delete_step(*args, **kwargs): try: yield # See the errback() function in the decorator() function. If a duplicate message is received, delete the step, so # that the collection is completable. Don't delete the step if the error was unexpected. except (AlreadyExists, InvalidFormError, IntegrityError): - _delete_step(*args, **kwargs) + _delete_step_and_finish(*args, **kwargs) raise else: - _delete_step(*args, **kwargs) - finally: - if finish: - finish(*finish_args) + _delete_step_and_finish(*args, **kwargs) -def _delete_step(step_type, **kwargs): +def _delete_step_and_finish(step_type, finish=None, finish_args=(), **kwargs): # kwargs can include collection_id, collection_file_id and ocid. processing_steps = ProcessingStep.objects.filter(name=step_type, **kwargs) @@ -150,3 +147,6 @@ def _delete_step(step_type, **kwargs): processing_steps.delete() else: logger.warning("No such processing step found: %s: %s", step_type, kwargs) + + if finish: + finish(*finish_args)