cronspec.go 724 B

123456789101112131415161718192021222324
  1. // Package cronspec parses the cron schedules an admin types into the settings.
  2. package cronspec
  3. import (
  4. "strings"
  5. "github.com/robfig/cron/v3"
  6. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  7. )
  8. // The panel scheduler runs WithSeconds(), whose parser rejects five-field crontab,
  9. // so admin schedules are parsed here with the seconds column optional.
  10. var parser = cron.NewParser(
  11. cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
  12. )
  13. func Parse(spec string) (cron.Schedule, error) {
  14. schedule, err := parser.Parse(strings.TrimSpace(spec))
  15. if err != nil {
  16. return nil, common.NewErrorf("cron schedule <%v> is not valid: %v", spec, err)
  17. }
  18. return schedule, nil
  19. }