32 lines
863 B
Go
32 lines
863 B
Go
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)
|
|
}
|