feat: add graph workflow fallback and retry metadata

This commit is contained in:
2026-04-07 10:56:45 +02:00
parent c17b7d2e8f
commit f9d4da52b9
9 changed files with 473 additions and 39 deletions
@@ -32,7 +32,15 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
from app.config import settings
from app.domains.orders.models import OrderLine
from app.domains.rendering.models import OutputType, WorkflowDefinition
from app.domains.rendering.workflow_config_utils import (
extract_runtime_workflow,
get_workflow_execution_mode,
)
from app.domains.rendering.workflow_executor import prepare_workflow_context
from app.domains.rendering.workflow_graph_runtime import (
execute_graph_workflow,
find_unsupported_graph_nodes,
)
from app.domains.rendering.workflow_run_service import create_workflow_run, mark_workflow_run_failed
engine = create_engine(
@@ -78,7 +86,90 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
)
return _legacy_dispatch(order_line_id)
from app.domains.rendering.workflow_config_utils import extract_runtime_workflow
execution_mode = get_workflow_execution_mode(wf_def.config, default="legacy")
if execution_mode == "graph":
try:
workflow_context = prepare_workflow_context(
wf_def.config,
context_id=order_line_id,
execution_mode="graph",
)
except Exception as exc:
logger.warning(
"order_line %s: workflow_definition_id %s failed graph runtime preparation (%s), "
"falling back to legacy dispatch",
order_line_id,
wf_def.id,
exc,
)
return _legacy_dispatch(order_line_id)
unsupported_nodes = find_unsupported_graph_nodes(workflow_context)
if unsupported_nodes:
logger.warning(
"order_line %s: workflow_definition_id %s contains graph-unsupported nodes %s, "
"falling back to legacy dispatch",
order_line_id,
wf_def.id,
unsupported_nodes,
)
return _legacy_dispatch(order_line_id)
run = None
try:
run = create_workflow_run(
session,
workflow_def_id=wf_def.id,
order_line_id=line.id,
workflow_context=workflow_context,
)
session.commit()
except Exception as exc:
session.rollback()
logger.warning(
"order_line %s: failed to create graph workflow run for workflow_definition_id %s (%s), "
"falling back to legacy dispatch",
order_line_id,
wf_def.id,
exc,
)
return _legacy_dispatch(order_line_id)
try:
dispatch_result = execute_graph_workflow(session, workflow_context)
session.commit()
except Exception as exc:
if run is not None:
mark_workflow_run_failed(run, str(exc))
session.commit()
logger.exception(
"order_line %s: graph workflow execution via definition %s failed, falling back to legacy dispatch",
order_line_id,
wf_def.id,
)
fallback_result = _legacy_dispatch(order_line_id)
fallback_result["fallback_from"] = "workflow_graph"
if run is not None:
fallback_result["workflow_run_id"] = str(run.id)
return fallback_result
return {
"backend": "workflow_graph",
"execution_mode": "graph",
"workflow_run_id": str(run.id),
"celery_task_id": dispatch_result.task_ids[0] if dispatch_result.task_ids else None,
"task_ids": dispatch_result.task_ids,
}
if execution_mode == "shadow":
logger.warning(
"order_line %s: workflow_definition_id %s requested shadow mode, "
"falling back to legacy dispatch until duplicate-safe shadow execution exists",
order_line_id,
wf_def.id,
)
return _legacy_dispatch(order_line_id)
workflow_type, params = extract_runtime_workflow(wf_def.config)
if workflow_type is None or workflow_type == "custom":
@@ -178,6 +269,7 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
return {
"backend": "workflow",
"workflow_type": workflow_type,
"execution_mode": "legacy",
"workflow_run_id": str(run.id),
"celery_task_id": celery_task_id,
}