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

scheduler: Rewrite complex Next and TickerDuration #55

Merged
merged 1 commit into from
Jun 7, 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
50 changes: 27 additions & 23 deletions scheduler/complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,12 @@ func (s multiSched) Next(t time.Time) (next time.Time) {
}

func (s multiSched) TickerDuration() time.Duration {
var downgrade bool
var res time.Duration
for _, i := range s {
d := gcd(res, i.TickerDuration())
if !downgrade && res == d {
downgrade = true
}
res = d
if l := len(s); l == 0 {
return 0
} else if l == 1 {
return s[0].TickerDuration()
}
if downgrade {
switch res {
case 24 * time.Hour:
return time.Hour
case time.Hour:
return time.Minute
case time.Minute:
return time.Second
}
}
return res
return time.Second
}

func (s multiSched) String() string {
Expand Down Expand Up @@ -137,12 +123,30 @@ func (s condSched) IsMatched(t time.Time) bool {
return true
}

func (s condSched) Next(t time.Time) time.Time {
if len(s) == 1 {
func (s condSched) Next(t time.Time) (next time.Time) {
if l := len(s); l == 0 {
return time.Time{}
} else if l == 1 {
return s[0].Next(t)
}
d := s.TickerDuration()
return t.Add(d).Truncate(d)
var start time.Time
var duration time.Duration
for _, s := range s {
if d := s.TickerDuration(); d > duration {
duration = d
start = s.Next(t)
} else if d == duration {
if next := s.Next(t); next.Before(start) {
start = next
}
}
}
for next = start; !s.IsMatched(next); next = next.Add(duration) {
if next.Sub(t) >= time.Hour*24*366 {
return time.Time{}
}
}
return
}

func (s condSched) TickerDuration() time.Duration {
Expand Down
20 changes: 1 addition & 19 deletions scheduler/complex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (

func TestMultiSchedule(t *testing.T) {
s := MultiSchedule(AtHour(3), AtMinute(4), AtSecond(5))
if d := s.TickerDuration(); d != time.Minute {
t.Fatalf("expected 1m: got %s", d)
}
if res := s.Next(time.Time{}).Format("15:04:05"); res != "00:00:05" {
t.Fatalf("expected 00:00:05: got %q", res)
}
Expand All @@ -28,27 +25,12 @@ func TestMultiSchedule(t *testing.T) {
}
}

func TestMultiScheduleTickerDuration(t *testing.T) {
for _, testcase := range []struct {
schedule Schedule
expected time.Duration
}{
{MultiSchedule(AtHour(3), AtMinute(4), AtSecond(5)), time.Minute},
{HourSchedule(9, 17, 23), time.Hour},
{MinuteSchedule(0, 59), time.Minute},
} {
if res := testcase.schedule.TickerDuration(); res != testcase.expected {
t.Errorf("expected %v; got %v", testcase.expected, res)
}
}
}

func TestConditionSchedule(t *testing.T) {
s := ConditionSchedule(Weekdays, MultiSchedule(AtClock(9, 30, 0), AtHour(15)))
if d := s.TickerDuration(); d != time.Second {
t.Fatalf("expected 1s: got %s", d)
}
if res := s.Next(time.Time{}).Format("15:04:05"); res != "00:00:01" {
if res := s.Next(time.Time{}).Format("15:04:05"); res != "09:30:00" {
t.Fatalf("expected 00:00:01: got %q", res)
}
for _, testcase := range []struct {
Expand Down
4 changes: 2 additions & 2 deletions scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestScheduler(t *testing.T) {
now := time.Now()
s := NewScheduler().At(TimeSchedule(now.Add(time.Second)))
s := NewScheduler().At(TimeSchedule(now.Add(2 * time.Second)))
defer s.Stop()
var n atomic.Int32
if err := s.Run(func(_ time.Time) { n.Add(1) }).Start(); err != nil {
Expand All @@ -17,7 +17,7 @@ func TestScheduler(t *testing.T) {
if n := n.Load(); n != 0 {
t.Errorf("expected 0; got %d", n)
}
time.Sleep(1500 * time.Millisecond)
time.Sleep(3 * time.Second)
if n := n.Load(); n != 1 {
t.Errorf("expected 1; got %d", n)
}
Expand Down
Loading