From c8a2799267b8877a36fe67b9904b580129786ecd Mon Sep 17 00:00:00 2001 From: Sherif Abdel-Naby Date: Sun, 4 Apr 2021 01:25:48 +0200 Subject: [PATCH] Add Schedule Interface + Fixed Intervals scheduler Signed-off-by: Sherif Abdel-Naby --- fixed.go | 29 +++++++++++++++++++++++++++++ schedule.go | 7 +++++++ 2 files changed, 36 insertions(+) create mode 100644 fixed.go create mode 100644 schedule.go diff --git a/fixed.go b/fixed.go new file mode 100644 index 0000000..09b62d1 --- /dev/null +++ b/fixed.go @@ -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 +} diff --git a/schedule.go b/schedule.go new file mode 100644 index 0000000..964d9e8 --- /dev/null +++ b/schedule.go @@ -0,0 +1,7 @@ +package main + +import "time" + +type Schedule interface { + Next() time.Time +}