package task import ( "bytes" "encoding/json" "time" "github.com/go-kratos/kratos/v3/errors" ) var ErrTaskExecutionOptions = errors.BadRequest("TASK_EXECUTION_OPTIONS_INVALID", "执行参数不正确:超时须为 1–86400 秒,重试次数为 0–10,重试间隔为 0–3600 秒") type ExecutionOptions struct { Timeout time.Duration MaxRetryTimes int RetryInterval time.Duration } // MethodExecutionOptions 承接迁移 SQL 保留的 _execution 元数据;普通方法维持原默认行为。 func MethodExecutionOptions(params []byte) (ExecutionOptions, error) { options := ExecutionOptions{Timeout: 5 * time.Minute, RetryInterval: time.Second} params = bytes.TrimSpace(params) if len(params) == 0 || params[0] != '{' { return options, nil } var envelope struct { Execution *struct { TimeoutSeconds *int64 `json:"timeoutSeconds"` MaxRetryTimes *int `json:"maxRetryTimes"` RetryIntervalSeconds *int64 `json:"retryIntervalSeconds"` } `json:"_execution"` } if err := json.Unmarshal(params, &envelope); err != nil { return options, ErrTaskExecutionOptions } if envelope.Execution == nil { return options, nil } value := envelope.Execution if value.TimeoutSeconds != nil { if *value.TimeoutSeconds < 1 || *value.TimeoutSeconds > 86400 { return options, ErrTaskExecutionOptions } options.Timeout = time.Duration(*value.TimeoutSeconds) * time.Second } if value.MaxRetryTimes != nil { if *value.MaxRetryTimes < 0 || *value.MaxRetryTimes > 10 { return options, ErrTaskExecutionOptions } options.MaxRetryTimes = *value.MaxRetryTimes } if value.RetryIntervalSeconds != nil { if *value.RetryIntervalSeconds < 0 || *value.RetryIntervalSeconds > 3600 { return options, ErrTaskExecutionOptions } options.RetryInterval = time.Duration(*value.RetryIntervalSeconds) * time.Second } return options, nil }