scheduler.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package cli // import "miniflux.app/v2/internal/cli"
  4. import (
  5. "log/slog"
  6. "time"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/storage"
  9. "miniflux.app/v2/internal/worker"
  10. )
  11. func runScheduler(store *storage.Storage, pool *worker.Pool) {
  12. slog.Debug(`Starting background scheduler...`)
  13. go feedScheduler(
  14. store,
  15. pool,
  16. config.Opts.PollingFrequency(),
  17. config.Opts.BatchSize(),
  18. config.Opts.PollingParsingErrorLimit(),
  19. config.Opts.PollingLimitPerHost(),
  20. )
  21. go cleanupScheduler(
  22. store,
  23. config.Opts.CleanupFrequency(),
  24. )
  25. }
  26. func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency time.Duration, batchSize, errorLimit, limitPerHost int) {
  27. for range time.Tick(frequency) {
  28. // Generate a batch of feeds for any user that has feeds to refresh.
  29. batchBuilder := store.NewBatchBuilder()
  30. batchBuilder.WithBatchSize(batchSize)
  31. batchBuilder.WithErrorLimit(errorLimit)
  32. batchBuilder.WithoutDisabledFeeds()
  33. batchBuilder.WithNextCheckExpired()
  34. batchBuilder.WithLimitPerHost(limitPerHost)
  35. if jobs, err := batchBuilder.FetchJobs(); err != nil {
  36. slog.Error("Unable to fetch jobs from database", slog.Any("error", err))
  37. } else if len(jobs) > 0 {
  38. slog.Debug("Feed URLs in this batch", slog.Any("feed_urls", jobs.FeedURLs()))
  39. pool.Push(jobs)
  40. }
  41. }
  42. }
  43. func cleanupScheduler(store *storage.Storage, frequency time.Duration) {
  44. for range time.Tick(frequency) {
  45. runCleanupTasks(store)
  46. }
  47. }