Skip to content

Commit 7060ecc

Browse files
gudvinrfguillot
authored andcommitted
refactor(cli): use time.Duration for scheduler frequency
Polling frequency is undocumented so it's not exacly clear what units were.
1 parent 4af12a4 commit 7060ecc

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

internal/cli/scheduler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ func runScheduler(store *storage.Storage, pool *worker.Pool) {
2626

2727
go cleanupScheduler(
2828
store,
29-
config.Opts.CleanupFrequencyHours(),
29+
config.Opts.CleanupFrequency(),
3030
)
3131
}
3232

33-
func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency, batchSize, errorLimit, limitPerHost int) {
34-
for range time.Tick(time.Duration(frequency) * time.Minute) {
33+
func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency time.Duration, batchSize, errorLimit, limitPerHost int) {
34+
for range time.Tick(frequency) {
3535
// Generate a batch of feeds for any user that has feeds to refresh.
3636
batchBuilder := store.NewBatchBuilder()
3737
batchBuilder.WithBatchSize(batchSize)
@@ -49,8 +49,8 @@ func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency, batchSi
4949
}
5050
}
5151

52-
func cleanupScheduler(store *storage.Storage, frequency int) {
53-
for range time.Tick(time.Duration(frequency) * time.Hour) {
52+
func cleanupScheduler(store *storage.Storage, frequency time.Duration) {
53+
for range time.Tick(frequency) {
5454
runCleanupTasks(store)
5555
}
5656
}

internal/config/config_test.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,22 @@ func TestDefaultCleanupFrequencyHoursValue(t *testing.T) {
589589
t.Fatalf(`Parsing failure: %v`, err)
590590
}
591591

592-
expected := defaultCleanupFrequencyHours
593-
result := opts.CleanupFrequencyHours()
592+
expected := defaultCleanupFrequency
593+
result := opts.CleanupFrequency()
594594

595595
if result != expected {
596596
t.Fatalf(`Unexpected CLEANUP_FREQUENCY_HOURS value, got %v instead of %v`, result, expected)
597597
}
598+
599+
sorted := opts.SortedOptions(false)
600+
i := slices.IndexFunc(sorted, func(opt *option) bool {
601+
return opt.Key == "CLEANUP_FREQUENCY_HOURS"
602+
})
603+
604+
expectedSerialized := int(defaultCleanupFrequency / time.Hour)
605+
if got := sorted[i].Value; got != expectedSerialized {
606+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
607+
}
598608
}
599609

600610
func TestCleanupFrequencyHours(t *testing.T) {
@@ -608,12 +618,22 @@ func TestCleanupFrequencyHours(t *testing.T) {
608618
t.Fatalf(`Parsing failure: %v`, err)
609619
}
610620

611-
expected := 42
612-
result := opts.CleanupFrequencyHours()
621+
expected := 42 * time.Hour
622+
result := opts.CleanupFrequency()
613623

614624
if result != expected {
615625
t.Fatalf(`Unexpected CLEANUP_FREQUENCY_HOURS value, got %v instead of %v`, result, expected)
616626
}
627+
628+
sorted := opts.SortedOptions(false)
629+
i := slices.IndexFunc(sorted, func(opt *option) bool {
630+
return opt.Key == "CLEANUP_FREQUENCY_HOURS"
631+
})
632+
633+
expectedSerialized := 42
634+
if got := sorted[i].Value; got != expectedSerialized {
635+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
636+
}
617637
}
618638

619639
func TestDefaultCleanupArchiveReadDaysValue(t *testing.T) {
@@ -737,6 +757,16 @@ func TestDefaultPollingFrequencyValue(t *testing.T) {
737757
if result != expected {
738758
t.Fatalf(`Unexpected POLLING_FREQUENCY value, got %v instead of %v`, result, expected)
739759
}
760+
761+
sorted := opts.SortedOptions(false)
762+
i := slices.IndexFunc(sorted, func(opt *option) bool {
763+
return opt.Key == "POLLING_FREQUENCY"
764+
})
765+
766+
expectedSerialized := int(defaultPollingFrequency / time.Minute)
767+
if got := sorted[i].Value; got != expectedSerialized {
768+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
769+
}
740770
}
741771

742772
func TestPollingFrequency(t *testing.T) {
@@ -749,12 +779,22 @@ func TestPollingFrequency(t *testing.T) {
749779
t.Fatalf(`Parsing failure: %v`, err)
750780
}
751781

752-
expected := 42
782+
expected := 42 * time.Minute
753783
result := opts.PollingFrequency()
754784

755785
if result != expected {
756786
t.Fatalf(`Unexpected POLLING_FREQUENCY value, got %v instead of %v`, result, expected)
757787
}
788+
789+
sorted := opts.SortedOptions(false)
790+
i := slices.IndexFunc(sorted, func(opt *option) bool {
791+
return opt.Key == "POLLING_FREQUENCY"
792+
})
793+
794+
expectedSerialized := 42
795+
if got := sorted[i].Value; got != expectedSerialized {
796+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
797+
}
758798
}
759799

760800
func TestDefaultForceRefreshInterval(t *testing.T) {

internal/config/options.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828
defaultRootURL = "http://localhost"
2929
defaultBasePath = ""
3030
defaultWorkerPoolSize = 16
31-
defaultPollingFrequency = 60
31+
defaultPollingFrequency = 60 * time.Minute
3232
defaultForceRefreshInterval = 30 * time.Second
3333
defaultBatchSize = 100
3434
defaultPollingScheduler = "round_robin"
@@ -47,7 +47,7 @@ const (
4747
defaultCertFile = ""
4848
defaultKeyFile = ""
4949
defaultCertDomain = ""
50-
defaultCleanupFrequencyHours = 24
50+
defaultCleanupFrequency = 24 * time.Hour
5151
defaultCleanupArchiveReadDays = 60
5252
defaultCleanupArchiveUnreadDays = 180
5353
defaultCleanupArchiveBatchSize = 10000
@@ -125,7 +125,7 @@ type options struct {
125125
certFile string
126126
certDomain string
127127
certKeyFile string
128-
cleanupFrequencyHours int
128+
cleanupFrequencyInterval time.Duration
129129
cleanupArchiveReadDays int
130130
cleanupArchiveUnreadDays int
131131
cleanupArchiveBatchSize int
@@ -137,7 +137,7 @@ type options struct {
137137
schedulerEntryFrequencyFactor int
138138
schedulerRoundRobinMinInterval time.Duration
139139
schedulerRoundRobinMaxInterval time.Duration
140-
pollingFrequency int
140+
pollingFrequency time.Duration
141141
pollingLimitPerHost int
142142
pollingParsingErrorLimit int
143143
pollingScheduler string
@@ -209,7 +209,7 @@ func NewOptions() *options {
209209
certFile: defaultCertFile,
210210
certDomain: defaultCertDomain,
211211
certKeyFile: defaultKeyFile,
212-
cleanupFrequencyHours: defaultCleanupFrequencyHours,
212+
cleanupFrequencyInterval: defaultCleanupFrequency,
213213
cleanupArchiveReadDays: defaultCleanupArchiveReadDays,
214214
cleanupArchiveUnreadDays: defaultCleanupArchiveUnreadDays,
215215
cleanupArchiveBatchSize: defaultCleanupArchiveBatchSize,
@@ -361,9 +361,9 @@ func (o *options) CertDomain() string {
361361
return o.certDomain
362362
}
363363

364-
// CleanupFrequencyHours returns the interval in hours for cleanup jobs.
365-
func (o *options) CleanupFrequencyHours() int {
366-
return o.cleanupFrequencyHours
364+
// CleanupFrequencyHours returns the interval for cleanup jobs.
365+
func (o *options) CleanupFrequency() time.Duration {
366+
return o.cleanupFrequencyInterval
367367
}
368368

369369
// CleanupArchiveReadDays returns the number of days after which marking read items as removed.
@@ -402,7 +402,7 @@ func (o *options) BatchSize() int {
402402
}
403403

404404
// PollingFrequency returns the interval to refresh feeds in the background.
405-
func (o *options) PollingFrequency() int {
405+
func (o *options) PollingFrequency() time.Duration {
406406
return o.pollingFrequency
407407
}
408408

@@ -721,10 +721,10 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
721721
"BATCH_SIZE": o.batchSize,
722722
"CERT_DOMAIN": o.certDomain,
723723
"CERT_FILE": o.certFile,
724+
"CLEANUP_FREQUENCY_HOURS": int(o.cleanupFrequencyInterval.Hours()),
724725
"CLEANUP_ARCHIVE_BATCH_SIZE": o.cleanupArchiveBatchSize,
725726
"CLEANUP_ARCHIVE_READ_DAYS": o.cleanupArchiveReadDays,
726727
"CLEANUP_ARCHIVE_UNREAD_DAYS": o.cleanupArchiveUnreadDays,
727-
"CLEANUP_FREQUENCY_HOURS": o.cleanupFrequencyHours,
728728
"CLEANUP_REMOVE_SESSIONS_DAYS": o.cleanupRemoveSessionsDays,
729729
"CREATE_ADMIN": o.createAdmin,
730730
"DATABASE_CONNECTION_LIFETIME": o.databaseConnectionLifetime,
@@ -770,7 +770,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
770770
"OAUTH2_USER_CREATION": o.oauth2UserCreationAllowed,
771771
"DISABLE_LOCAL_AUTH": o.disableLocalAuth,
772772
"FORCE_REFRESH_INTERVAL": int(o.forceRefreshInterval.Seconds()),
773-
"POLLING_FREQUENCY": o.pollingFrequency,
773+
"POLLING_FREQUENCY": int(o.pollingFrequency.Minutes()),
774774
"POLLING_LIMIT_PER_HOST": o.pollingLimitPerHost,
775775
"POLLING_PARSING_ERROR_LIMIT": o.pollingParsingErrorLimit,
776776
"POLLING_SCHEDULER": o.pollingScheduler,

internal/config/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (p *parser) parseLines(lines []string) (err error) {
127127
case "CERT_DOMAIN":
128128
p.opts.certDomain = parseString(value, defaultCertDomain)
129129
case "CLEANUP_FREQUENCY_HOURS":
130-
p.opts.cleanupFrequencyHours = parseInt(value, defaultCleanupFrequencyHours)
130+
p.opts.cleanupFrequencyInterval = parseInterval(value, time.Hour, defaultCleanupFrequency)
131131
case "CLEANUP_ARCHIVE_READ_DAYS":
132132
p.opts.cleanupArchiveReadDays = parseInt(value, defaultCleanupArchiveReadDays)
133133
case "CLEANUP_ARCHIVE_UNREAD_DAYS":
@@ -143,7 +143,7 @@ func (p *parser) parseLines(lines []string) (err error) {
143143
case "BATCH_SIZE":
144144
p.opts.batchSize = parseInt(value, defaultBatchSize)
145145
case "POLLING_FREQUENCY":
146-
p.opts.pollingFrequency = parseInt(value, defaultPollingFrequency)
146+
p.opts.pollingFrequency = parseInterval(value, time.Minute, defaultPollingFrequency)
147147
case "POLLING_LIMIT_PER_HOST":
148148
p.opts.pollingLimitPerHost = parseInt(value, 0)
149149
case "POLLING_PARSING_ERROR_LIMIT":

0 commit comments

Comments
 (0)