feat: add workflow rollout gate signals
This commit is contained in:
@@ -17,6 +17,28 @@ import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _build_rollout_signal(
|
||||
*,
|
||||
gate_status: str,
|
||||
ready: bool,
|
||||
reasons: list[str],
|
||||
workflow_def_id=None,
|
||||
output_type_id=None,
|
||||
verdict: str | None = None,
|
||||
) -> dict:
|
||||
return {
|
||||
"rollout_gate_status": gate_status,
|
||||
"rollout_gate_verdict": verdict,
|
||||
"rollout_gate_reasons": reasons,
|
||||
"workflow_rollout_ready": ready,
|
||||
"workflow_rollout_status": "ready_for_rollout" if ready else "hold_legacy_authoritative",
|
||||
"output_type_rollout_ready": ready,
|
||||
"output_type_rollout_status": "ready_for_rollout" if ready else "hold_legacy_authoritative",
|
||||
"rollout_workflow_definition_id": str(workflow_def_id) if workflow_def_id is not None else None,
|
||||
"rollout_output_type_id": str(output_type_id) if output_type_id is not None else None,
|
||||
}
|
||||
|
||||
|
||||
def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
"""Dispatch a render for the given order line.
|
||||
|
||||
@@ -33,6 +55,7 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
from app.domains.orders.models import OrderLine
|
||||
from app.domains.rendering.models import OutputType, WorkflowDefinition
|
||||
from app.domains.rendering.workflow_config_utils import (
|
||||
canonicalize_workflow_config,
|
||||
extract_runtime_workflow,
|
||||
get_workflow_execution_mode,
|
||||
)
|
||||
@@ -67,7 +90,16 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
"order_line %s: no workflow_definition_id, using legacy dispatch",
|
||||
order_line_id,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="legacy_only",
|
||||
ready=False,
|
||||
reasons=["No workflow definition is linked; legacy dispatch remains authoritative."],
|
||||
output_type_id=getattr(output_type, "id", None),
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
# Load the linked WorkflowDefinition
|
||||
wf_def: WorkflowDefinition | None = session.execute(
|
||||
@@ -84,13 +116,45 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
order_line_id,
|
||||
output_type.workflow_definition_id,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_unavailable",
|
||||
ready=False,
|
||||
reasons=["Linked workflow definition is missing or inactive; legacy dispatch remains authoritative."],
|
||||
workflow_def_id=output_type.workflow_definition_id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
execution_mode = get_workflow_execution_mode(wf_def.config, default="legacy")
|
||||
try:
|
||||
canonical_config = canonicalize_workflow_config(wf_def.config)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"order_line %s: workflow_definition_id %s has invalid config (%s), "
|
||||
"falling back to legacy dispatch",
|
||||
order_line_id,
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_invalid",
|
||||
ready=False,
|
||||
reasons=[f"Workflow definition config is invalid: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
execution_mode = get_workflow_execution_mode(canonical_config, default="legacy")
|
||||
|
||||
def _prepare_graph_context(target_mode: str):
|
||||
workflow_context = prepare_workflow_context(
|
||||
wf_def.config,
|
||||
canonical_config,
|
||||
context_id=order_line_id,
|
||||
execution_mode=target_mode,
|
||||
)
|
||||
@@ -122,7 +186,18 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result["fallback_from"] = "workflow_graph"
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="graph_preparation_failed",
|
||||
ready=False,
|
||||
reasons=[f"Graph runtime preparation failed: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
run = None
|
||||
try:
|
||||
@@ -136,7 +211,18 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result["fallback_from"] = "workflow_graph"
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="graph_run_creation_failed",
|
||||
ready=False,
|
||||
reasons=[f"Graph workflow run creation failed: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
try:
|
||||
dispatch_result = execute_graph_workflow(session, workflow_context)
|
||||
@@ -154,15 +240,35 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
fallback_result = _legacy_dispatch(order_line_id)
|
||||
fallback_result["fallback_from"] = "workflow_graph"
|
||||
fallback_result["workflow_run_id"] = str(run.id)
|
||||
fallback_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="graph_execution_failed",
|
||||
ready=False,
|
||||
reasons=[f"Graph workflow execution failed: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return fallback_result
|
||||
|
||||
return {
|
||||
result = {
|
||||
"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,
|
||||
}
|
||||
result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="graph_authoritative",
|
||||
ready=True,
|
||||
verdict="pass",
|
||||
reasons=["Workflow graph dispatch is authoritative for this output type."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
if execution_mode == "shadow":
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
@@ -180,6 +286,18 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
legacy_result["execution_mode"] = "shadow"
|
||||
legacy_result["shadow_status"] = "skipped"
|
||||
legacy_result["shadow_error"] = str(exc)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="shadow_skipped",
|
||||
ready=False,
|
||||
reasons=[
|
||||
"Shadow workflow preparation failed; legacy dispatch remains authoritative.",
|
||||
f"Preparation error: {exc}.",
|
||||
],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
run = None
|
||||
@@ -197,6 +315,18 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
legacy_result["execution_mode"] = "shadow"
|
||||
legacy_result["shadow_status"] = "failed"
|
||||
legacy_result["shadow_error"] = str(exc)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="shadow_run_creation_failed",
|
||||
ready=False,
|
||||
reasons=[
|
||||
"Shadow workflow run could not be created; legacy dispatch remains authoritative.",
|
||||
f"Run creation error: {exc}.",
|
||||
],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
try:
|
||||
@@ -216,15 +346,39 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
legacy_result["shadow_status"] = "failed"
|
||||
legacy_result["shadow_error"] = str(exc)
|
||||
legacy_result["shadow_workflow_run_id"] = str(run.id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="shadow_execution_failed",
|
||||
ready=False,
|
||||
reasons=[
|
||||
"Shadow workflow execution failed; legacy dispatch remains authoritative.",
|
||||
f"Execution error: {exc}.",
|
||||
],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
legacy_result["execution_mode"] = "shadow"
|
||||
legacy_result["shadow_status"] = "dispatched"
|
||||
legacy_result["shadow_workflow_run_id"] = str(run.id)
|
||||
legacy_result["shadow_task_ids"] = dispatch_result.task_ids
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="pending_shadow_verdict",
|
||||
ready=False,
|
||||
reasons=[
|
||||
"Legacy dispatch remains authoritative until the shadow workflow comparison returns pass.",
|
||||
"A pass verdict is required before workflow-first rollout is ready.",
|
||||
],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
workflow_type, params = extract_runtime_workflow(wf_def.config)
|
||||
workflow_type, params = extract_runtime_workflow(canonical_config)
|
||||
if workflow_type is None or workflow_type == "custom":
|
||||
logger.warning(
|
||||
"order_line %s: workflow_definition_id %s has no supported preset runtime, "
|
||||
@@ -232,7 +386,17 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
order_line_id,
|
||||
wf_def.id,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_runtime_unsupported",
|
||||
ready=False,
|
||||
reasons=["Workflow definition has no supported preset runtime; legacy dispatch remains authoritative."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
logger.info(
|
||||
"order_line %s: dispatching via WorkflowDefinition %s (type=%s)",
|
||||
@@ -243,7 +407,7 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
|
||||
try:
|
||||
workflow_context = prepare_workflow_context(
|
||||
wf_def.config,
|
||||
canonical_config,
|
||||
context_id=order_line_id,
|
||||
execution_mode="legacy",
|
||||
)
|
||||
@@ -255,7 +419,17 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_preparation_failed",
|
||||
ready=False,
|
||||
reasons=[f"Workflow runtime preparation failed: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
# For turntable workflows: resolve step_path + output_dir from the order line at runtime
|
||||
if workflow_type == "turntable" and ("step_path" not in params or "output_dir" not in params):
|
||||
@@ -299,7 +473,17 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_run_creation_failed",
|
||||
ready=False,
|
||||
reasons=[f"Workflow run creation failed: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
from app.domains.rendering.workflow_builder import dispatch_workflow
|
||||
|
||||
@@ -317,15 +501,35 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
order_line_id,
|
||||
wf_def.id,
|
||||
)
|
||||
return _legacy_dispatch(order_line_id)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
legacy_result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_dispatch_failed",
|
||||
ready=False,
|
||||
reasons=[f"Workflow dispatch failed: {exc}."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return legacy_result
|
||||
|
||||
return {
|
||||
result = {
|
||||
"backend": "workflow",
|
||||
"workflow_type": workflow_type,
|
||||
"execution_mode": "legacy",
|
||||
"workflow_run_id": str(run.id),
|
||||
"celery_task_id": celery_task_id,
|
||||
}
|
||||
result.update(
|
||||
_build_rollout_signal(
|
||||
gate_status="workflow_legacy_runtime",
|
||||
ready=False,
|
||||
reasons=["Workflow definition is active, but execution still uses the legacy runtime path."],
|
||||
workflow_def_id=wf_def.id,
|
||||
output_type_id=output_type.id,
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def _legacy_dispatch(order_line_id: str) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user