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

fix: fix cron schedule Contains check #4

Merged
merged 4 commits into from
Aug 21, 2024
Merged
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
3 changes: 2 additions & 1 deletion config/crd/bases/wing.xscaling.dev_replicaautoscalers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ spec:
type: object
properties:
threshold:
description:
description: Threshold allowed pending for exhaust mode.
anyOf:
- type: integer
- type: string
x-kubernetes-int-or-string: true
timeoutSeconds:
type: integer
type: object
Expand Down
1 change: 1 addition & 0 deletions core/scheduling/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func GetScheduledSettingsRaw(when time.Time, settings wingv1.TargetSettings) (pa
}

if scheduler.Contains(when) {
hitScheduleSettingsPayload = make([]byte, len(schedule.Settings.Raw))
copy(hitScheduleSettingsPayload, schedule.Settings.Raw)
break
}
Expand Down
54 changes: 54 additions & 0 deletions core/scheduling/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
)

func isSchedulePeriodContains(when time.Time, scheduleSettings wingv1.ScheduleTargetSettings) (bool, error) {
Expand Down Expand Up @@ -272,3 +273,56 @@ func BenchmarkIsSchedulePeriodContainsWithDate(b *testing.B) {
})
}
}

func TestGetScheduledSettingsRaw(t *testing.T) {
settings := wingv1.TargetSettings{
Default: &runtime.RawExtension{
Raw: []byte(`{"a":"b","c":"d"}`),
},
Schedules: []wingv1.ScheduleTargetSettings{
{
Start: "0 8 * * *",
End: "0 10 * * *",
Timezone: "Asia/Shanghai",
Settings: &runtime.RawExtension{
Raw: []byte(`{"a":"x","e":"f"}`),
},
},
},
}
for _, c := range []struct {
date string
payload string
}{
{
date: "2021-01-01 7:59",
payload: `{"a":"b","c":"d"}`,
},
{
date: "2021-01-01 8:00",
payload: `{"a":"x","c":"d","e":"f"}`,
},
{
date: "2021-01-01 9:00",
payload: `{"a":"x","c":"d","e":"f"}`,
},
{
date: "2021-01-01 9:59",
payload: `{"a":"x","c":"d","e":"f"}`,
},
{
date: "2021-01-01 10:00",
payload: `{"a":"b","c":"d"}`,
},
{
date: "2021-01-01 10:01",
payload: `{"a":"b","c":"d"}`,
},
} {
date, err := time.ParseInLocation(timerange.SchedulePeriodDateFormat, c.date, time.Local)
require.NoError(t, err)
payload, err := GetScheduledSettingsRaw(date, settings)
require.NoError(t, err)
require.Equal(t, c.payload, string(payload))
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
sigs.k8s.io/controller-runtime v0.13.1
)

replace github.com/robfig/cron/v3 v3.0.1 => github.com/juliev0/cron/v3 v3.0.2-0.20220310063235-7181f74c09e9 // https://github.com/robfig/cron/pull/437

require (
cloud.google.com/go v0.97.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/juliev0/cron/v3 v3.0.2-0.20220310063235-7181f74c09e9 h1:Pa9SanmckPLZ4HeHEl/OGxXPKRg5NpNx3xmM8wDN/LE=
github.com/juliev0/cron/v3 v3.0.2-0.20220310063235-7181f74c09e9/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -310,8 +312,6 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
22 changes: 5 additions & 17 deletions utils/timerange/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,11 @@ func NewCronScheduler(timezone *time.Location, start, end string) (*CronSchedule
return s, nil
}

func (s *CronScheduler) GetUpcomingTriggerDuration(when time.Time) (start, end time.Time) {
whenInTimezone := when.In(s.timezone)
return s.startSched.Next(whenInTimezone), s.endSched.Next(whenInTimezone)
}

func (s *CronScheduler) Contains(when time.Time) bool {
whenInTimezone := when.In(s.timezone)

start, end := s.GetUpcomingTriggerDuration(whenInTimezone)
var (
nextStartTimestamp = start.Unix()
nextEndTimestamp = end.Unix()
currentTimestamp = whenInTimezone.Unix()
)
// current timestamp always before next start timestamp
// so if current timestamp is before next end timestamp(strong requirement) and next start timestamp is after next end timestamp
// then current timestamp is in the range of duration.
// In short, current timestamp before end but already started.
return nextStartTimestamp > nextEndTimestamp && currentTimestamp <= nextEndTimestamp
lastStart := s.startSched.Prev(whenInTimezone)
nextStart := s.startSched.Next(whenInTimezone)
nextEnd := s.endSched.Next(whenInTimezone)
// when in [lastStart, nextEnd) and nextStart > nextEnd
return whenInTimezone.After(lastStart) && whenInTimezone.Before(nextEnd) && nextStart.After(nextEnd)
}
Loading