Files
httprunner/step_function.go
lilong.129 5f400735fc fix: 修复 StartToGoal 命令无法通过 CTRL+C 中断的问题
- 为 AI 相关方法添加 context.Context 参数支持中断

- 在重试循环中添加上下文取消检查

- 创建可取消的上下文并监听中断信号

- 更新 MCP 工具调用使用带上下文的方法

现在用户可以通过 CTRL+C 正常中断长时间运行的 AI 自动化任务
2025-06-05 20:00:20 +08:00

81 lines
1.5 KiB
Go

package hrp
import (
"fmt"
"os"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/uixt"
)
// StepFunction implements IStep interface.
type StepFunction struct {
StepConfig
Fn func()
}
func (s *StepFunction) Name() string {
return s.StepName
}
func (s *StepFunction) Type() StepType {
return StepTypeFunction
}
func (s *StepFunction) Config() *StepConfig {
return &StepConfig{
StepName: s.StepName,
Variables: s.Variables,
}
}
func (s *StepFunction) Run(r *SessionRunner) (*StepResult, error) {
return runStepFunction(r, s)
}
func runStepFunction(r *SessionRunner, step IStep) (stepResult *StepResult, err error) {
var fn func()
switch stepFn := step.(type) {
case *StepFunction:
fn = stepFn.Fn
default:
return nil, errors.New("unexpected function step type")
}
log.Info().
Str("name", step.Name()).
Str("type", string(StepTypeFunction)).
Msg("run function")
start := time.Now()
stepResult = &StepResult{
Name: step.Name(),
StepType: step.Type(),
Success: false,
ContentSize: 0,
StartTime: start.Unix(),
}
defer func() {
attachments := uixt.Attachments{}
if err != nil {
attachments["error"] = err.Error()
}
stepResult.Attachments = attachments
stepResult.Elapsed = time.Since(start).Milliseconds()
}()
vars := r.caseRunner.Config.Get().Variables
for key, value := range vars {
os.Setenv(key, fmt.Sprintf("%v", value))
}
// exec function
fn()
stepResult.Success = true
return stepResult, nil
}