Skip to content

Commit

Permalink
Simplify cron scaler code
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Smith <joelsmith@redhat.com>
  • Loading branch information
joelsmith committed Aug 9, 2024
1 parent b57db3a commit 0c09b4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ New deprecation(s):
### Other

- **General**: Bump deps and k8s deps to 0.29.7 ([#6035](https://github.com/kedacore/keda/pull/6035))
- **Cron Scaler**: Simplify code to determine next start and stop times ([#6056](https://github.com/kedacore/keda/pull/6056))

## v2.15.0

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

0 comments on commit 0c09b4b

Please sign in to comment.