scheduler.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. jobs, err := store.NewBatchBuilder().
  30. WithBatchSize(batchSize).
  31. WithErrorLimit(errorLimit).
  32. WithoutDisabledFeeds().
  33. WithNextCheckExpired().
  34. WithLimitPerHost(limitPerHost).
  35. FetchJobs()
  36. if err != nil {
  37. slog.Error("Unable to fetch jobs from database", slog.Any("error", err))
  38. } else if len(jobs) > 0 {
  39. slog.Debug("Feed URLs in this batch", slog.Any("feed_urls", jobs.FeedURLs()))
  40. pool.Push(jobs)
  41. }
  42. }
  43. }
  44. func cleanupScheduler(store *storage.Storage, frequency time.Duration) {
  45. for range time.Tick(frequency) {
  46. runCleanupTasks(store)
  47. }
  48. }