Safe asserts (#180)

This commit is contained in:
jbirddog 2023-03-14 20:22:57 -04:00 committed by GitHub
parent 1f2845962c
commit 569995aa46
2 changed files with 27 additions and 6 deletions

View File

@ -0,0 +1,18 @@
"""Assertion_service."""
import contextlib
from typing import Generator
import sentry_sdk
from flask import current_app
@contextlib.contextmanager
def safe_assertion(condition: bool) -> Generator[bool, None, None]:
try:
yield True
except AssertionError as e:
if not condition:
sentry_sdk.capture_exception(e)
current_app.logger.exception(e)
if current_app.config["ENV_IDENTIFIER"] == "local_development":
raise e

View File

@ -4,7 +4,6 @@ from typing import Callable
from typing import List
from typing import Optional
from flask import current_app
from SpiffWorkflow.bpmn.serializer.workflow import BpmnWorkflowSerializer # type: ignore
from SpiffWorkflow.bpmn.workflow import BpmnWorkflow # type: ignore
from SpiffWorkflow.exceptions import SpiffWorkflowException # type: ignore
@ -20,6 +19,7 @@ from spiffworkflow_backend.models.message_instance_correlation import (
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
from spiffworkflow_backend.models.spiff_step_details import SpiffStepDetailsModel
from spiffworkflow_backend.models.task import TaskModel # noqa: F401
from spiffworkflow_backend.services.assertion_service import safe_assertion
from spiffworkflow_backend.services.process_instance_lock_service import (
ProcessInstanceLockService,
)
@ -290,11 +290,14 @@ class WorkflowExecutionService:
def do_engine_steps(self, exit_at: None = None, save: bool = False) -> None:
"""Do_engine_steps."""
if not ProcessInstanceLockService.has_lock(self.process_instance_model.id):
# TODO: can't be an exception yet - believe there are flows that are not locked.
current_app.logger.error(
"The current thread has not obtained a lock for this process instance.",
)
with safe_assertion(
ProcessInstanceLockService.has_lock(self.process_instance_model.id)
) as tripped:
if tripped:
raise AssertionError(
"The current thread has not obtained a lock for this process"
f" instance ({self.process_instance_model.id})."
)
try:
self.bpmn_process_instance.refresh_waiting_tasks()