// Package dbrefresh runs a scanner's vulnerability database update on a fixed interval. package dbrefresh import ( "context" "time" "github.com/rs/zerolog/log" ) // DefaultInterval applies when the configured interval is 0. const DefaultInterval = 6 * time.Hour // Run calls update every interval until stop closes. immediate runs the first update at once; // otherwise it waits one interval. func Run(stop <-chan struct{}, interval time.Duration, immediate bool, scanner string, update func(context.Context) error) { if interval <= 0 { interval = DefaultInterval } ctx, cancel := context.WithCancel(context.Background()) defer cancel() go func() { <-stop cancel() }() once := func() { if err := update(ctx); err != nil { log.Error().Err(err).Str("scanner", scanner).Msg("Scheduled database update failed") } } if immediate { once() } ticker := time.NewTicker(interval) defer ticker.Stop() for { select { case <-ticker.C: once() case <-ctx.Done(): return } } }