mirror of
https://github.com/Fishwaldo/sched.git
synced 2025-07-11 07:29:25 +00:00
Add Schedule Interface + Fixed Intervals scheduler
Signed-off-by: Sherif Abdel-Naby <sherifabdlnaby@gmail.com>
This commit is contained in:
parent
980d96a4da
commit
c8a2799267
2 changed files with 36 additions and 0 deletions
29
fixed.go
Normal file
29
fixed.go
Normal 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
7
schedule.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Schedule interface {
|
||||||
|
Next() time.Time
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue