Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify cron scaler code #6056

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
run: make clientset-verify

- name: Verify generated Manifests are up to date
run: make verify-manifests
run: rm -f bin/controller-gen && make verify-manifests

- name: Build
run: make build
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ New deprecation(s):

### Other

- TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))
- **Cron Scaler**: Simplify code to determine next start and stop times ([#6056](https://github.com/kedacore/keda/pull/6056))
joelsmith marked this conversation as resolved.
Show resolved Hide resolved

## v2.15.1

Expand Down
31 changes: 9 additions & 22 deletions pkg/scalers/cron_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type cronScaler struct {
type cronMetadata struct {
start string
end string
startSched cron.Schedule
endSched cron.Schedule
timezone string
desiredReplicas int64
triggerIndex int
Expand All @@ -54,21 +56,12 @@ func NewCronScaler(config *scalersconfig.ScalerConfig) (Scaler, error) {
}, nil
}

func getCronTime(location *time.Location, spec string) (int64, error) {
c := cron.New(cron.WithLocation(location))
_, err := c.AddFunc(spec, func() { _ = fmt.Sprintf("Cron initialized for location %s", location.String()) })
if err != nil {
return 0, err
}

c.Start()
cronTime := c.Entries()[0].Next.Unix()
c.Stop()

return cronTime, nil
func getCronTime(location *time.Location, sched cron.Schedule) int64 {
return sched.Next(time.Now().In(location)).Unix()
}

func parseCronMetadata(config *scalersconfig.ScalerConfig) (*cronMetadata, error) {
var err error
if len(config.TriggerMetadata) == 0 {
return nil, fmt.Errorf("invalid Input Metadata. %s", config.TriggerMetadata)
}
Expand All @@ -81,7 +74,7 @@ func parseCronMetadata(config *scalersconfig.ScalerConfig) (*cronMetadata, error
}
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
if val, ok := config.TriggerMetadata["start"]; ok && val != "" {
_, err := parser.Parse(val)
meta.startSched, err = parser.Parse(val)
if err != nil {
return nil, fmt.Errorf("error parsing start schedule: %w", err)
}
Expand All @@ -90,7 +83,7 @@ func parseCronMetadata(config *scalersconfig.ScalerConfig) (*cronMetadata, error
return nil, fmt.Errorf("no start schedule specified. %s", config.TriggerMetadata)
}
if val, ok := config.TriggerMetadata["end"]; ok && val != "" {
_, err := parser.Parse(val)
meta.endSched, err = parser.Parse(val)
if err != nil {
return nil, fmt.Errorf("error parsing end schedule: %w", err)
}
Expand Down Expand Up @@ -152,15 +145,9 @@ func (s *cronScaler) GetMetricsAndActivity(_ context.Context, metricName string)
// Since we are considering the timestamp here and not the exact time, timezone does matter.
currentTime := time.Now().Unix()

nextStartTime, startTimecronErr := getCronTime(location, s.metadata.start)
if startTimecronErr != nil {
return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("error initializing start cron: %w", startTimecronErr)
}
nextStartTime := getCronTime(location, s.metadata.startSched)

nextEndTime, endTimecronErr := getCronTime(location, s.metadata.end)
if endTimecronErr != nil {
return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("error intializing end cron: %w", endTimecronErr)
}
nextEndTime := getCronTime(location, s.metadata.endSched)

switch {
case nextStartTime < nextEndTime && currentTime < nextStartTime:
Expand Down
Loading