update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
24
scheduler/log_adapter.go
Normal file
24
scheduler/log_adapter.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"github.com/navidrome/navidrome/log"
|
||||
)
|
||||
|
||||
type logger struct{}
|
||||
|
||||
func (l *logger) Info(msg string, keysAndValues ...interface{}) {
|
||||
args := []interface{}{
|
||||
"Scheduler: " + msg,
|
||||
}
|
||||
args = append(args, keysAndValues...)
|
||||
log.Debug(args...)
|
||||
}
|
||||
|
||||
func (l *logger) Error(err error, msg string, keysAndValues ...interface{}) {
|
||||
args := []interface{}{
|
||||
"Scheduler: " + msg,
|
||||
}
|
||||
args = append(args, keysAndValues...)
|
||||
args = append(args, err)
|
||||
log.Error(args...)
|
||||
}
|
||||
45
scheduler/scheduler.go
Normal file
45
scheduler/scheduler.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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()) (int, error)
|
||||
Remove(id int)
|
||||
}
|
||||
|
||||
func GetInstance() Scheduler {
|
||||
return singleton.GetInstance(func() *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()) (int, error) {
|
||||
entryID, err := s.c.AddFunc(crontab, cmd)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(entryID), nil
|
||||
}
|
||||
|
||||
func (s *scheduler) Remove(id int) {
|
||||
s.c.Remove(cron.EntryID(id))
|
||||
}
|
||||
86
scheduler/scheduler_test.go
Normal file
86
scheduler/scheduler_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/tests"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func TestScheduler(t *testing.T) {
|
||||
tests.Init(t, false)
|
||||
log.SetLevel(log.LevelFatal)
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Scheduler Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("Scheduler", func() {
|
||||
var s *scheduler
|
||||
|
||||
BeforeEach(func() {
|
||||
c := cron.New(cron.WithLogger(&logger{}))
|
||||
s = &scheduler{c: c}
|
||||
s.c.Start() // Start the scheduler for tests
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
s.c.Stop() // Stop the scheduler after tests
|
||||
})
|
||||
|
||||
It("adds and executes a job", func() {
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
||||
executed := false
|
||||
id, err := s.Add("@every 100ms", func() {
|
||||
executed = true
|
||||
wg.Done()
|
||||
})
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(id).ToNot(BeZero())
|
||||
|
||||
wg.Wait()
|
||||
Expect(executed).To(BeTrue())
|
||||
})
|
||||
|
||||
It("removes a job", func() {
|
||||
// Use a WaitGroup to ensure the job executes once
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
||||
counter := 0
|
||||
id, err := s.Add("@every 100ms", func() {
|
||||
counter++
|
||||
if counter == 1 {
|
||||
wg.Done() // Signal that the job has executed once
|
||||
}
|
||||
})
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(id).ToNot(BeZero())
|
||||
|
||||
// Wait for the job to execute at least once
|
||||
wg.Wait()
|
||||
|
||||
// Verify job executed
|
||||
Expect(counter).To(Equal(1))
|
||||
|
||||
// Remove the job
|
||||
s.Remove(id)
|
||||
|
||||
// Store the counter value
|
||||
currentCount := counter
|
||||
|
||||
// Wait some time to ensure job doesn't execute again
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
// Verify counter didn't increase
|
||||
Expect(counter).To(Equal(currentCount))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user