This commit is contained in:
anhduy-tech
2026-02-11 01:38:57 +07:00
parent 8d8a7bdb80
commit f94611f973
6 changed files with 376 additions and 423 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,12 +5,12 @@ from app.workflow_utils import resolve_value
@transaction.atomic
def execute_step(step: StepAction, context: dict):
print(f"\n>>> EXECUTING STEP: {step.step_code} (Order: {step.order})")
#print(f"\n>>> EXECUTING STEP: {step.step_code} (Order: {step.order})")
# Evaluate rules first
for rule in step.rules.filter(is_active=True):
if not evaluate_rule(rule, context):
print(f"Step {step.step_code} skipped due to rule failure.")
#print(f"Step {step.step_code} skipped due to rule failure.")
return {"step": step.step_code, "skipped": True, "reason": "rule_failed"}
results = []
@@ -21,10 +21,10 @@ def execute_step(step: StepAction, context: dict):
action_type = action.get("type")
params = action.get("params", {})
print(f" - Action Type: {action_type}")
#print(f" - Action Type: {action_type}")
if action_type not in ACTION_REGISTRY:
print(f" - ERROR: Action type '{action_type}' not registered!")
#print(f" - ERROR: Action type '{action_type}' not registered!")
continue
try:
@@ -39,7 +39,7 @@ def execute_step(step: StepAction, context: dict):
# Lưu output cuối cùng vào context
context["last_result"] = output
except Exception as e:
print(f" - ERROR in action {action_type}: {str(e)}")
#print(f" - ERROR in action {action_type}: {str(e)}")
# Raise để transaction.atomic rollback nếu cần, hoặc xử lý tùy ý
raise e
@@ -52,7 +52,7 @@ def evaluate_rule(rule: Rule, context: dict):
right = resolve_value(condition.get("right"), context)
op = condition.get("operator", "==")
print(f" Evaluating Rule: {left} {op} {right}")
#print(f" Evaluating Rule: {left} {op} {right}")
if op == "IN" and left not in right: return False
if op == "==" and left != right: return False
@@ -64,21 +64,21 @@ def evaluate_rule(rule: Rule, context: dict):
def run_workflow(workflow_code: str, trigger: str, context: dict):
print(f"\n================ START WORKFLOW: {workflow_code} ================")
print(f"Trigger: {trigger} | Initial Context: {context}")
#print(f"\n================ START WORKFLOW: {workflow_code} ================")
#print(f"Trigger: {trigger} | Initial Context: {context}")
workflow = Workflow.objects.filter(code=workflow_code, is_active=True).first()
if not workflow:
print(f"Workflow '{workflow_code}' not found or inactive.")
#print(f"Workflow '{workflow_code}' not found or inactive.")
raise Exception(f"Workflow '{workflow_code}' not found")
steps = workflow.steps.filter(trigger_event=trigger, is_active=True).order_by("order")
print(f"Found {steps.count()} active steps.")
#print(f"Found {steps.count()} active steps.")
outputs = []
for step in steps:
res = execute_step(step, context)
outputs.append(res)
print(f"================ FINISH WORKFLOW: {workflow_code} ================\n")
#print(f"================ FINISH WORKFLOW: {workflow_code} ================\n")
return outputs

Binary file not shown.