From c18c5a8d9c4546685d49fc132a8fd26834e2657a Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Wed, 25 Oct 2023 13:45:20 -0400 Subject: [PATCH] Feature/do not save cancelled task events twice (#575) * do not save cancelled task events again * actually only process cancelled events that were cancelled during the current run --------- Co-authored-by: jasquat --- .../services/task_service.py | 1 + .../services/workflow_execution_service.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py index 0a6d3a399..1effacebe 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/task_service.py @@ -213,6 +213,7 @@ class TaskService: event_type = ProcessInstanceEventType.task_completed.value if task_model.state == "CANCELLED": event_type = ProcessInstanceEventType.task_cancelled.value + timestamp = task_model.end_in_seconds or task_model.start_in_seconds or time.time() ( process_instance_event, diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/workflow_execution_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/workflow_execution_service.py index 475c6dec4..6a3ddeb20 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/workflow_execution_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/workflow_execution_service.py @@ -201,6 +201,8 @@ class TaskModelSavingDelegate(EngineStepDelegate): self.spiff_tasks_to_process: set[UUID] = set() self.spiff_task_timestamps: dict[UUID, StartAndEndTimes] = {} + self.run_started_at = time.time() + self.task_service = TaskService( process_instance=self.process_instance, serializer=self.serializer, @@ -250,7 +252,6 @@ class TaskModelSavingDelegate(EngineStepDelegate): # 1ead87b4b496525df8cc0e27836c3e987d593dc0 if you are curious. for waiting_spiff_task in bpmn_process_instance.get_tasks( state=TaskState.WAITING - | TaskState.CANCELLED | TaskState.READY | TaskState.MAYBE | TaskState.LIKELY @@ -260,6 +261,18 @@ class TaskModelSavingDelegate(EngineStepDelegate): ): self.task_service.update_task_model_with_spiff_task(waiting_spiff_task) + # only process cancelled tasks that were cancelled during this run + # NOTE: this could mean we do not add task models that we should be adding + # in which case we may have to remove the updated_ts filter here and + # instead just avoid creating the event in update_task_model_with_spiff_task + cancelled_spiff_tasks = bpmn_process_instance.get_tasks( + state=TaskState.CANCELLED, updated_ts=self.run_started_at + ) + for cancelled_spiff_task in cancelled_spiff_tasks: + self.task_service.update_task_model_with_spiff_task( + spiff_task=cancelled_spiff_task, + ) + self.task_service.save_objects_to_database() if self.secondary_engine_step_delegate: