diff --git a/config.yml b/config.yml index 69f64d0..79bff63 100644 --- a/config.yml +++ b/config.yml @@ -23,8 +23,8 @@ log: # 数据库配置 (PostgreSQL) database: - host: "huangwc.com" - port: 5432 + host: "192.168.5.16" + port: 5431 username: "pig-farm-controller" password: "pig-farm-controller" dbname: "pig-farm-controller" diff --git a/internal/infra/utils/time.go b/internal/infra/utils/time.go index b66e39a..fed5705 100644 --- a/internal/infra/utils/time.go +++ b/internal/infra/utils/time.go @@ -6,18 +6,12 @@ import ( "github.com/robfig/cron/v3" ) -// cronParserWithSeconds 是一个自定义的 cron 解析器,它包含了秒字段。 -// 这允许解析6部分的 cron 表达式(带秒),并处理 '?' 通配符。 -var cronParserWithSeconds = cron.NewParser( - cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor, -) - // GetNextCronTime 根据传入的 Cron 表达式计算下一次执行的时间。 -// 它使用一个支持秒级精度和'?'通配符的解析器。 +// 它使用兼容大多数 Cron 实现的标准解析器。 // 如果 Cron 表达式无效,它将返回一个错误。 func GetNextCronTime(cronExpression string) (time.Time, error) { - // 使用自定义的解析器来解析表达式 - schedule, err := cronParserWithSeconds.Parse(cronExpression) + // cron.ParseStandard() 返回一个支持标准5位或6位(带秒)格式的解析器。 + schedule, err := cron.ParseStandard(cronExpression) if err != nil { return time.Time{}, err // 返回零值时间和错误 } diff --git a/internal/infra/utils/time_test.go b/internal/infra/utils/time_test.go deleted file mode 100644 index dd645e8..0000000 --- a/internal/infra/utils/time_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package utils_test - -import ( - "testing" - "time" - - "git.huangwc.com/pig/pig-farm-controller/internal/infra/utils" -) - -func TestGetNextCronTime(t *testing.T) { - tests := []struct { - name string - expression string - expectError bool - }{ - { - name: "标准5位cron表达式", - expression: "0 15 10 * * *", - expectError: false, - }, - { - name: "带秒的6位cron表达式", - expression: "30 0 15 10 * *", - expectError: false, - }, - { - name: "带'?'通配符的cron表达式", - expression: "0 15 10 * * ?", - expectError: false, - }, - { - name: "带秒和'?'通配符的cron表达式", - expression: "30 0 15 10 * ?", - expectError: false, - }, - { - name: "无效的cron表达式", - expression: "invalid-cron", - expectError: true, - }, - { - name: "每秒执行", - expression: "* * * * * *", - expectError: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - nextTime, err := utils.GetNextCronTime(tt.expression) - if tt.expectError { - if err == nil { - t.Errorf("期望得到一个错误,但没有得到") - } - } else { - if err != nil { - t.Errorf("不期望得到错误,但得到了: %v", err) - } - if nextTime.IsZero() { - t.Errorf("期望得到一个有效的下一次执行时间,但得到了零值时间") - } - // 可以在这里添加更具体的断言来验证时间是否在预期范围内 - if !nextTime.After(time.Now()) { - t.Errorf("期望的下一次执行时间 %v 应该在当前时间之后", nextTime) - } - } - }) - } -}