diff --git a/pkg/reconciler/config.go b/pkg/reconciler/config.go index f0de8ee31..a1b3d9b15 100644 --- a/pkg/reconciler/config.go +++ b/pkg/reconciler/config.go @@ -21,17 +21,36 @@ type ConfigWatcher struct { job gocron.Job scheduler gocron.Scheduler handlerFunc func() + jobFactoryFunc func(string) gocron.JobDefinition watcher *fsnotify.Watcher } func NewConfigWatcher(configPath string, scheduler gocron.Scheduler, configWatcher *fsnotify.Watcher, handlerFunc func()) (*ConfigWatcher, error) { + return newConfigWatcher( + configPath, + scheduler, + configWatcher, + func(schedule string) gocron.JobDefinition { + return gocron.CronJob(schedule, false) + }, + handlerFunc, + ) +} + +func newConfigWatcher( + configPath string, + scheduler gocron.Scheduler, + configWatcher *fsnotify.Watcher, + cronJobFactoryFunc func(string) gocron.JobDefinition, + handlerFunc func(), +) (*ConfigWatcher, error) { schedule, err := determineCronExpression(configPath) if err != nil { return nil, err } job, err := scheduler.NewJob( - gocron.CronJob(schedule, false), + cronJobFactoryFunc(schedule), gocron.NewTask(handlerFunc), ) if err != nil { @@ -46,6 +65,7 @@ func NewConfigWatcher(configPath string, scheduler gocron.Scheduler, configWatch scheduler: scheduler, watcher: configWatcher, handlerFunc: handlerFunc, + jobFactoryFunc: cronJobFactoryFunc, }, nil } @@ -100,7 +120,7 @@ func (c *ConfigWatcher) syncConfig(relevantEventPredicate func(event fsnotify.Ev } updatedJob, err := c.scheduler.Update( c.job.ID(), - gocron.CronJob(updatedSchedule, false), + c.jobFactoryFunc(updatedSchedule), gocron.NewTask(c.handlerFunc), ) if err != nil { diff --git a/pkg/reconciler/config_test.go b/pkg/reconciler/config_test.go index 55906904f..c5bb1fa95 100644 --- a/pkg/reconciler/config_test.go +++ b/pkg/reconciler/config_test.go @@ -29,18 +29,18 @@ var _ = Describe("Reconciler configuration watcher", func() { configDir, err = os.MkdirTemp("", "config") Expect(err).NotTo(HaveOccurred()) const ( - initialCron = "0/1 2 3 * *" - dummyFileName = "DUMMY" + initialCronWithSeconds = "0/1 2 3 * * *" + dummyFileName = "DUMMY" ) dummyConfig, err = os.Create(filepath.Join(configDir, filepath.Base(dummyFileName))) Expect(err).NotTo(HaveOccurred()) - Expect(dummyConfig.Write([]byte(initialCron))).To(Equal(len(initialCron))) + Expect(dummyConfig.Write([]byte(initialCronWithSeconds))).To(Equal(len(initialCronWithSeconds))) scheduler, err := gocron.NewScheduler() Expect(err).NotTo(HaveOccurred()) watcher, err = fsnotify.NewWatcher() Expect(err).NotTo(HaveOccurred()) - config, err = NewConfigWatcher( + config, err = newConfigWatcherForTests( dummyConfig.Name(), scheduler, watcher, @@ -59,15 +59,27 @@ var _ = Describe("Reconciler configuration watcher", func() { }) When("the cron job expression is updated in the file-system", func() { - const updatedCron = "0/1 * * * *" + const updatedCronWithSeconds = "0/1 * * * * *" BeforeEach(func() { - Expect(dummyConfig.WriteAt([]byte(updatedCron), 0)).To(Equal(len(updatedCron))) + Expect(dummyConfig.WriteAt([]byte(updatedCronWithSeconds), 0)).To(Equal(len(updatedCronWithSeconds))) }) It("the current schedule is updated, and the handler function executed", func() { - Eventually(func() string { return config.currentSchedule }).Should(Equal(updatedCron)) + Eventually(func() string { return config.currentSchedule }).Should(Equal(updatedCronWithSeconds)) Eventually(mailbox).WithTimeout(time.Minute).Should(Receive()) }) }) }) + +func newConfigWatcherForTests(configPath string, scheduler gocron.Scheduler, configWatcher *fsnotify.Watcher, handlerFunc func()) (*ConfigWatcher, error) { + return newConfigWatcher( + configPath, + scheduler, + configWatcher, + func(schedule string) gocron.JobDefinition { + return gocron.CronJob(schedule, true) + }, + handlerFunc, + ) +}