feat(scheduler): add crontab(5) random ~ syntax support (#5233)
* feat(scheduler): add CrontabSchedule with crontab(5) random ~ syntax Implement ParseCrontab() that extends robfig/cron with support for the crontab(5) random ~ operator (e.g., 0~30 * * * *). Random values are resolved fresh on each Next() call for load spreading. Supports A~B, ~B, A~, and bare ~ forms in all 6 fields (including seconds). Expressions without ~ delegate to robfig's standard parser with zero overhead. Integrates into scheduler.Add() and conf.validateSchedule() so that scanner.schedule and backup.schedule config values accept ~ syntax. * refactor(scheduler): resolve random ~ values once at parse time Change from per-Next() randomization to per-parse randomization, matching crontab(5) semantics. This prevents double-firing within the same period when random values land after the current time. ParseCrontab now resolves ~ fields to concrete values, substitutes them into the spec string, and delegates to robfig's parser. This eliminates CrontabSchedule, randomField, and resolveField entirely. * test(scheduler): replace WaitGroup with channel for job execution synchronization Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
@@ -33,10 +33,11 @@ func (s *scheduler) Run(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (s *scheduler) Add(crontab string, cmd func()) (int, error) {
|
||||
entryID, err := s.c.AddFunc(crontab, cmd)
|
||||
schedule, err := ParseCrontab(crontab)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
entryID := s.c.Schedule(schedule, cron.FuncJob(cmd))
|
||||
return int(entryID), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user