Add Schedule Interface + Fixed Intervals scheduler

Signed-off-by: Sherif Abdel-Naby <sherifabdlnaby@gmail.com>
This commit is contained in:
Sherif Abdel-Naby 2021-04-04 01:25:48 +02:00
parent 980d96a4da
commit c8a2799267
2 changed files with 36 additions and 0 deletions

29
fixed.go Normal file
View file

@ -0,0 +1,29 @@
package main
import (
"fmt"
"time"
)
type Fixed struct {
duration time.Duration
next time.Time
}
func NewFixed(duration time.Duration) (*Fixed, error) {
if duration < 0 {
return nil, fmt.Errorf("invalid duration, must be >= 0")
}
return &Fixed{
duration: duration,
next: time.Now().Add(duration),
}, nil
}
func (f *Fixed) Next() time.Time {
now := time.Now()
if now.After(f.next) {
f.next = f.next.Add(f.duration)
}
return f.next
}

7
schedule.go Normal file
View file

@ -0,0 +1,7 @@
package main
import "time"
type Schedule interface {
Next() time.Time
}