feat: add notifier scheduler and NotifyOnce sent-tracking
This commit is contained in:
31
internal/notifier/cron_schedule.go
Normal file
31
internal/notifier/cron_schedule.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
// CronSchedule wraps a robfig/cron schedule to satisfy the notifier.Schedule
|
||||
// interface used by StartScheduler. The spec follows the standard 5-field cron
|
||||
// syntax (e.g. "0 9 * * *" for daily at 09:00 in the process local time).
|
||||
type CronSchedule struct {
|
||||
spec string
|
||||
c cron.Schedule
|
||||
}
|
||||
|
||||
// NewCronSchedule parses a cron spec and returns a Schedule. An error is
|
||||
// returned if the spec is not a valid cron expression.
|
||||
func NewCronSchedule(spec string) (*CronSchedule, error) {
|
||||
c, err := cron.ParseStandard(spec)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse cron schedule %q: %w", spec, err)
|
||||
}
|
||||
return &CronSchedule{spec: spec, c: c}, nil
|
||||
}
|
||||
|
||||
// Next returns the next time the schedule fires after t.
|
||||
func (s *CronSchedule) Next(t time.Time) time.Time {
|
||||
return s.c.Next(t)
|
||||
}
|
||||
Reference in New Issue
Block a user