worker.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package worker // import "miniflux.app/v2/internal/worker"
  4. import (
  5. "log/slog"
  6. "sync"
  7. "time"
  8. "miniflux.app/v2/internal/config"
  9. "miniflux.app/v2/internal/metric"
  10. "miniflux.app/v2/internal/model"
  11. feedHandler "miniflux.app/v2/internal/reader/handler"
  12. "miniflux.app/v2/internal/storage"
  13. )
  14. type worker struct {
  15. id int
  16. store *storage.Storage
  17. }
  18. // Run processes feed refresh jobs from the channel until the pool is shut down.
  19. func (w *worker) Run(c <-chan model.Job, shutdown <-chan struct{}, wg *sync.WaitGroup) {
  20. defer wg.Done()
  21. slog.Debug("Worker started",
  22. slog.Int("worker_id", w.id),
  23. )
  24. for {
  25. var job model.Job
  26. select {
  27. case <-shutdown:
  28. return
  29. case job = <-c:
  30. }
  31. slog.Debug("Job received by worker",
  32. slog.Int("worker_id", w.id),
  33. slog.Int64("user_id", job.UserID),
  34. slog.Int64("feed_id", job.FeedID),
  35. slog.String("feed_url", job.FeedURL),
  36. )
  37. startTime := time.Now()
  38. localizedError := feedHandler.RefreshFeed(w.store, job.UserID, job.FeedID, false)
  39. if config.Opts.HasMetricsCollector() {
  40. status := metric.StatusSuccess
  41. if localizedError != nil {
  42. status = metric.StatusError
  43. }
  44. metric.BackgroundFeedRefreshDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  45. }
  46. }
  47. }