fix: 完成商务迁移复核与接口优化并清理历史资料
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMethodExecutionOptionsPreservesImportedPolicy(t *testing.T) {
|
||||
options, err := MethodExecutionOptions([]byte(`{"startDate":"2026-11-03","_execution":{"timeoutSeconds":1600,"maxRetryTimes":3,"retryIntervalSeconds":1}}`))
|
||||
if err != nil || options.Timeout != 1600*time.Second || options.MaxRetryTimes != 3 || options.RetryInterval != time.Second {
|
||||
t.Fatalf("迁移任务策略丢失: %+v %v", options, err)
|
||||
}
|
||||
for _, params := range []string{"", `{}`, `[]`, `"value"`} {
|
||||
options, err := MethodExecutionOptions([]byte(params))
|
||||
if err != nil || options.Timeout != 5*time.Minute || options.MaxRetryTimes != 0 {
|
||||
t.Fatalf("普通方法默认行为变化: %q %+v %v", params, options, err)
|
||||
}
|
||||
}
|
||||
for _, params := range []string{`{"_execution":{"timeoutSeconds":0}}`, `{"_execution":{"timeoutSeconds":86401}}`, `{"_execution":{"maxRetryTimes":11}}`, `{"_execution":{"retryIntervalSeconds":-1}}`, `{"_execution":"invalid"}`} {
|
||||
if _, err := MethodExecutionOptions([]byte(params)); err == nil {
|
||||
t.Fatalf("无效执行参数未拒绝: %s", params)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,6 +108,9 @@ func (uc *TaskUsecase) Validate(value *TimedTask) error {
|
||||
if len(value.Params) > 0 && !json.Valid(value.Params) {
|
||||
return errors.New("params 必须是合法 JSON")
|
||||
}
|
||||
if _, err := MethodExecutionOptions(value.Params); err != nil {
|
||||
return err
|
||||
}
|
||||
case TaskExecutorHTTP:
|
||||
parsed, parseErr := url.Parse(value.HTTPURL)
|
||||
if parseErr != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" {
|
||||
|
||||
Reference in New Issue
Block a user