diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/jinja_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/jinja_service.py index 8007f724..97f7601c 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/jinja_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/jinja_service.py @@ -8,6 +8,7 @@ from SpiffWorkflow.task import Task as SpiffTask # type: ignore from spiffworkflow_backend.exceptions.api_error import ApiError from spiffworkflow_backend.models.task import TaskModel # noqa: F401 +from spiffworkflow_backend.models.task_instructions_for_end_user import TaskInstructionsForEndUserModel from spiffworkflow_backend.services.task_service import TaskModelError from spiffworkflow_backend.services.task_service import TaskService @@ -101,3 +102,25 @@ class JinjaService: tb = tb.tb_next wfe.add_note("Jinja2 template errors can happen when trying to display task data") raise wfe from error + + @classmethod + def add_instruction_for_end_user_if_appropriate( + cls, spiff_tasks: list[SpiffTask], process_instance_id: int, tasks_that_have_been_seen: set[str] + ) -> None: + for spiff_task in spiff_tasks: + if spiff_task.task_spec.manual: + continue + if hasattr(spiff_task.task_spec, "extensions") and spiff_task.task_spec.extensions.get( + "instructionsForEndUser", None + ): + task_guid = str(spiff_task.id) + if task_guid in tasks_that_have_been_seen: + continue + instruction = JinjaService.render_instructions_for_end_user(spiff_task) + if instruction != "": + TaskInstructionsForEndUserModel.insert_or_update_record( + task_guid=str(spiff_task.id), + process_instance_id=process_instance_id, + instruction=instruction, + ) + tasks_that_have_been_seen.add(str(spiff_task.id)) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_service.py index ead3d5ec..a34832a9 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_service.py @@ -44,6 +44,7 @@ from spiffworkflow_backend.services.authorization_service import AuthorizationSe from spiffworkflow_backend.services.error_handling_service import ErrorHandlingService from spiffworkflow_backend.services.git_service import GitCommandError from spiffworkflow_backend.services.git_service import GitService +from spiffworkflow_backend.services.jinja_service import JinjaService from spiffworkflow_backend.services.process_instance_processor import CustomBpmnScriptEngine from spiffworkflow_backend.services.process_instance_processor import ProcessInstanceProcessor from spiffworkflow_backend.services.process_instance_queue_service import ProcessInstanceIsAlreadyLockedError @@ -478,16 +479,19 @@ class ProcessInstanceService: processor.complete_task(spiff_task, human_task, user=user) # the caller needs to handle the actual queueing of the process instance for better dequeueing ability - if not should_queue_process_instance(processor.process_instance_model, execution_mode): - if not ProcessInstanceTmpService.is_enqueued_to_run_in_the_future(processor.process_instance_model): - with sentry_sdk.start_span(op="task", description="backend_do_engine_steps"): - execution_strategy_name = None - if execution_mode == ProcessInstanceExecutionMode.synchronous.value: - execution_strategy_name = "greedy" + if should_queue_process_instance(processor.process_instance_model, execution_mode): + processor.bpmn_process_instance.refresh_waiting_tasks() + tasks = processor.bpmn_process_instance.get_tasks(state=TaskState.WAITING | TaskState.READY) + JinjaService.add_instruction_for_end_user_if_appropriate(tasks, processor.process_instance_model.id, set()) + elif not ProcessInstanceTmpService.is_enqueued_to_run_in_the_future(processor.process_instance_model): + with sentry_sdk.start_span(op="task", description="backend_do_engine_steps"): + execution_strategy_name = None + if execution_mode == ProcessInstanceExecutionMode.synchronous.value: + execution_strategy_name = "greedy" - # maybe move this out once we have the interstitial page since this is - # here just so we can get the next human task - processor.do_engine_steps(save=True, execution_strategy_name=execution_strategy_name) + # maybe move this out once we have the interstitial page since this is + # here just so we can get the next human task + processor.do_engine_steps(save=True, execution_strategy_name=execution_strategy_name) @staticmethod def spiff_task_to_api_task( 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 58a7464c..8b314987 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/workflow_execution_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/workflow_execution_service.py @@ -33,7 +33,6 @@ from spiffworkflow_backend.models.message_instance import MessageInstanceModel from spiffworkflow_backend.models.message_instance_correlation import MessageInstanceCorrelationRuleModel from spiffworkflow_backend.models.process_instance import ProcessInstanceModel from spiffworkflow_backend.models.process_instance_event import ProcessInstanceEventType -from spiffworkflow_backend.models.task_instructions_for_end_user import TaskInstructionsForEndUserModel from spiffworkflow_backend.models.user import UserModel from spiffworkflow_backend.services.assertion_service import safe_assertion from spiffworkflow_backend.services.jinja_service import JinjaService @@ -344,21 +343,7 @@ class QueueInstructionsForEndUserExecutionStrategy(ExecutionStrategy): def should_do_before(self, bpmn_process_instance: BpmnWorkflow, process_instance_model: ProcessInstanceModel) -> None: tasks = bpmn_process_instance.get_tasks(state=TaskState.WAITING | TaskState.READY) - for spiff_task in tasks: - if hasattr(spiff_task.task_spec, "extensions") and spiff_task.task_spec.extensions.get( - "instructionsForEndUser", None - ): - task_guid = str(spiff_task.id) - if task_guid in self.tasks_that_have_been_seen: - continue - instruction = JinjaService.render_instructions_for_end_user(spiff_task) - if instruction != "": - TaskInstructionsForEndUserModel.insert_or_update_record( - task_guid=str(spiff_task.id), - process_instance_id=process_instance_model.id, - instruction=instruction, - ) - self.tasks_that_have_been_seen.add(str(spiff_task.id)) + JinjaService.add_instruction_for_end_user_if_appropriate(tasks, process_instance_model.id, self.tasks_that_have_been_seen) def should_break_before(self, tasks: list[SpiffTask], process_instance_model: ProcessInstanceModel) -> bool: for spiff_task in tasks: