mirror of
https://github.com/navidrome/navidrome.git
synced 2024-09-11 20:12:54 +02:00
39 lines
666 B
Go
39 lines
666 B
Go
package scheduler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/navidrome/navidrome/utils/singleton"
|
|
"github.com/robfig/cron/v3"
|
|
)
|
|
|
|
type Scheduler interface {
|
|
Run(ctx context.Context)
|
|
Add(crontab string, cmd func()) error
|
|
}
|
|
|
|
func GetInstance() Scheduler {
|
|
instance := singleton.Get(&scheduler{}, func() interface{} {
|
|
c := cron.New(cron.WithLogger(&logger{}))
|
|
return &scheduler{
|
|
c: c,
|
|
}
|
|
})
|
|
return instance.(*scheduler)
|
|
}
|
|
|
|
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
|
|
}
|