platform-core/modules/shared/workflowState.py
ValueOn AG 4a60086c80
Some checks failed
Deploy Plattform-Core (Int) / test (push) Failing after 15s
Deploy Plattform-Core (Int) / deploy (push) Has been skipped
cp adapted to 2026 poweron
2026-06-09 09:53:31 +02:00

47 lines
1.7 KiB
Python

# Copyright (c) 2026 PowerOn AG
# All rights reserved.
"""
Workflow State
Shared utilities for workflow state management and validation.
"""
import logging
from typing import Any
logger = logging.getLogger(__name__)
class WorkflowStoppedException(Exception):
"""Exception raised when a workflow is stopped by the user."""
pass
def checkWorkflowStopped(services: Any) -> None:
"""
Check if workflow has been stopped by user and raise exception if so.
Args:
services: Services object with workflow and interfaceDbChat for fresh status check
Raises:
WorkflowStoppedException: If workflow status is "stopped"
"""
workflow = getattr(services, 'workflow', None)
if not workflow or not hasattr(workflow, 'id') or workflow.id is None:
return
try:
# Get the current workflow status from the database to avoid stale data
currentWorkflow = services.chat.getWorkflow(workflow.id)
if currentWorkflow and currentWorkflow.status == "stopped":
logger.info("Workflow stopped by user, aborting operation")
raise WorkflowStoppedException("Workflow was stopped by user")
except WorkflowStoppedException:
# Re-raise the stop signal immediately
raise
except Exception as e:
# If we can't get the current status due to other database issues, fall back to the in-memory object
logger.warning(f"Could not check current workflow status from database: {str(e)}")
if workflow and workflow.status == "stopped":
logger.info("Workflow stopped by user (from in-memory object), aborting operation")
raise WorkflowStoppedException("Workflow was stopped by user")