Breaking change: Add ScanSchedule, allows interval and cron based configurations.
See https://pkg.go.dev/github.com/robfig/cron#hdr-CRON_Expression_Format for expression syntax. `ScanInterval` will still work for the time being. The only situation it does not work is when you want to disable periodic scanning by setting `ScanInterval=0`. If you want to disable it, please set `ScanSchedule=""` Closes #1085
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type Scheduler interface {
|
||||
Run(ctx context.Context)
|
||||
Add(crontab string, cmd func()) error
|
||||
}
|
||||
|
||||
func New() Scheduler {
|
||||
c := cron.New(cron.WithLogger(&logger{}))
|
||||
return &scheduler{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
type scheduler struct {
|
||||
c *cron.Cron
|
||||
}
|
||||
|
||||
func (s *scheduler) Run(ctx context.Context) {
|
||||
s.c.Start()
|
||||
<-ctx.Done()
|
||||
s.c.Stop()
|
||||
}
|
||||
|
||||
func (s *scheduler) Add(crontab string, cmd func()) error {
|
||||
_, err := s.c.AddFunc(crontab, cmd)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user