createExecution 创建执行实例
(def *WorkflowDefinition, inputs map[string]any)
| 399 | |
| 400 | // createExecution 创建执行实例 |
| 401 | func (e *Engine) createExecution(def *WorkflowDefinition, inputs map[string]any) (*WorkflowExecution, error) { |
| 402 | executionID := generateExecutionID() |
| 403 | |
| 404 | ctx, cancel := context.WithCancel(context.Background()) |
| 405 | |
| 406 | // 创建会话 |
| 407 | session, err := e.sessionManager.CreateSession(ctx, def.ID) |
| 408 | if err != nil { |
| 409 | cancel() |
| 410 | return nil, fmt.Errorf("failed to create session: %w", err) |
| 411 | } |
| 412 | |
| 413 | // 创建工作流上下文 |
| 414 | workflowCtx := &WorkflowContext{ |
| 415 | WorkflowID: def.ID, |
| 416 | ExecutionID: executionID, |
| 417 | StartTime: time.Now(), |
| 418 | Status: StatusPending, |
| 419 | Variables: make(map[string]any), |
| 420 | Inputs: inputs, |
| 421 | Outputs: make(map[string]any), |
| 422 | Completed: make(map[string]bool), |
| 423 | Failed: make(map[string]error), |
| 424 | Metadata: make(map[string]any), |
| 425 | Context: ctx, |
| 426 | Session: session, |
| 427 | } |
| 428 | |
| 429 | execution := &WorkflowExecution{ |
| 430 | ID: executionID, |
| 431 | WorkflowID: def.ID, |
| 432 | Definition: def, |
| 433 | Status: StatusPending, |
| 434 | Context: workflowCtx, |
| 435 | CurrentNodes: []string{}, |
| 436 | CompletedNodes: make(map[string]bool), |
| 437 | FailedNodes: make(map[string]error), |
| 438 | NodeResults: make(map[string]*NodeResult), |
| 439 | StartTime: time.Now(), |
| 440 | LastActivity: time.Now(), |
| 441 | cancelFunc: cancel, |
| 442 | done: make(chan struct{}), |
| 443 | Errors: make([]WorkflowError, 0), |
| 444 | Warnings: make([]string, 0), |
| 445 | } |
| 446 | |
| 447 | // 注册执行 |
| 448 | e.executionsMu.Lock() |
| 449 | e.executions[executionID] = execution |
| 450 | e.executionsMu.Unlock() |
| 451 | |
| 452 | // 发布开始事件 |
| 453 | if e.eventBus != nil { |
| 454 | _ = e.eventBus.Publish(ctx, &WorkflowEvent{ |
| 455 | Type: "workflow.started", |
| 456 | ExecutionID: executionID, |
| 457 | NodeID: "", |
| 458 | Timestamp: time.Now(), |
no test coverage detected