package action import ( "context" "fmt" "time" ) func init() { Register("delay", func() Action { return &delayAction{} }) } // delayAction 延迟指定毫秒数(响应 context 取消)。 type delayAction struct{} func (a *delayAction) ID() string { return "delay" } func (a *delayAction) Name() string { return "延迟" } func (a *delayAction) Schema() ActionSchema { return ActionSchema{Params: []ParamSpec{ {Name: "ms", Label: "延迟(毫秒)", Type: "int", Required: true, Help: "动作链中等待的毫秒数(>=0)"}, }} } func (a *delayAction) Validate(params map[string]any) error { if ms := GetInt(params, "ms"); ms < 0 { return fmt.Errorf("delay: ms must be >= 0") } return nil } func (a *delayAction) Execute(ctx context.Context, params map[string]any) error { ms := GetInt(params, "ms") timer := time.NewTimer(time.Duration(ms) * time.Millisecond) defer timer.Stop() select { case <-ctx.Done(): return ctx.Err() case <-timer.C: return nil } }