feat: add duplicate-safe workflow shadow dispatch
This commit is contained in:
@@ -88,13 +88,32 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
|
||||
execution_mode = get_workflow_execution_mode(wf_def.config, default="legacy")
|
||||
|
||||
def _prepare_graph_context(target_mode: str):
|
||||
workflow_context = prepare_workflow_context(
|
||||
wf_def.config,
|
||||
context_id=order_line_id,
|
||||
execution_mode=target_mode,
|
||||
)
|
||||
unsupported_nodes = find_unsupported_graph_nodes(workflow_context)
|
||||
if unsupported_nodes:
|
||||
raise ValueError(
|
||||
f"graph-unsupported nodes present: {', '.join(unsupported_nodes)}"
|
||||
)
|
||||
return workflow_context
|
||||
|
||||
def _create_graph_run(workflow_context):
|
||||
run = create_workflow_run(
|
||||
session,
|
||||
workflow_def_id=wf_def.id,
|
||||
order_line_id=line.id,
|
||||
workflow_context=workflow_context,
|
||||
)
|
||||
session.commit()
|
||||
return run
|
||||
|
||||
if execution_mode == "graph":
|
||||
try:
|
||||
workflow_context = prepare_workflow_context(
|
||||
wf_def.config,
|
||||
context_id=order_line_id,
|
||||
execution_mode="graph",
|
||||
)
|
||||
workflow_context = _prepare_graph_context("graph")
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"order_line %s: workflow_definition_id %s failed graph runtime preparation (%s), "
|
||||
@@ -105,26 +124,9 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
)
|
||||
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()
|
||||
run = _create_graph_run(workflow_context)
|
||||
except Exception as exc:
|
||||
session.rollback()
|
||||
logger.warning(
|
||||
@@ -140,9 +142,10 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
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()
|
||||
session.rollback()
|
||||
session.add(run)
|
||||
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,
|
||||
@@ -150,8 +153,7 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
)
|
||||
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)
|
||||
fallback_result["workflow_run_id"] = str(run.id)
|
||||
return fallback_result
|
||||
|
||||
return {
|
||||
@@ -163,13 +165,64 @@ def dispatch_render_with_workflow(order_line_id: str) -> dict:
|
||||
}
|
||||
|
||||
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)
|
||||
legacy_result = _legacy_dispatch(order_line_id)
|
||||
|
||||
try:
|
||||
workflow_context = _prepare_graph_context("shadow")
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"order_line %s: shadow graph preparation for workflow_definition_id %s failed (%s), "
|
||||
"continuing with authoritative legacy dispatch only",
|
||||
order_line_id,
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
legacy_result["execution_mode"] = "shadow"
|
||||
legacy_result["shadow_status"] = "skipped"
|
||||
legacy_result["shadow_error"] = str(exc)
|
||||
return legacy_result
|
||||
|
||||
run = None
|
||||
try:
|
||||
run = _create_graph_run(workflow_context)
|
||||
except Exception as exc:
|
||||
session.rollback()
|
||||
logger.warning(
|
||||
"order_line %s: failed to create shadow workflow run for workflow_definition_id %s (%s); "
|
||||
"legacy dispatch remains authoritative",
|
||||
order_line_id,
|
||||
wf_def.id,
|
||||
exc,
|
||||
)
|
||||
legacy_result["execution_mode"] = "shadow"
|
||||
legacy_result["shadow_status"] = "failed"
|
||||
legacy_result["shadow_error"] = str(exc)
|
||||
return legacy_result
|
||||
|
||||
try:
|
||||
dispatch_result = execute_graph_workflow(session, workflow_context)
|
||||
session.commit()
|
||||
except Exception as exc:
|
||||
session.rollback()
|
||||
session.add(run)
|
||||
mark_workflow_run_failed(run, str(exc))
|
||||
session.commit()
|
||||
logger.exception(
|
||||
"order_line %s: shadow workflow execution via definition %s failed; legacy dispatch remains authoritative",
|
||||
order_line_id,
|
||||
wf_def.id,
|
||||
)
|
||||
legacy_result["execution_mode"] = "shadow"
|
||||
legacy_result["shadow_status"] = "failed"
|
||||
legacy_result["shadow_error"] = str(exc)
|
||||
legacy_result["shadow_workflow_run_id"] = str(run.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
|
||||
return legacy_result
|
||||
|
||||
workflow_type, params = extract_runtime_workflow(wf_def.config)
|
||||
if workflow_type is None or workflow_type == "custom":
|
||||
|
||||
Reference in New Issue
Block a user