worker.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "miniflux.app/v2/internal/urllib"
  14. )
  15. type worker struct {
  16. id int
  17. store *storage.Storage
  18. }
  19. // Run processes feed refresh jobs from the channel until the pool is shut down.
  20. func (w *worker) Run(c <-chan model.Job, shutdown <-chan struct{}, wg *sync.WaitGroup) {
  21. defer wg.Done()
  22. slog.Debug("Worker started",
  23. slog.Int("worker_id", w.id),
  24. )
  25. for {
  26. var job model.Job
  27. select {
  28. case <-shutdown:
  29. return
  30. case job = <-c:
  31. }
  32. slog.Debug("Job received by worker",
  33. slog.Int("worker_id", w.id),
  34. slog.Int64("user_id", job.UserID),
  35. slog.Int64("feed_id", job.FeedID),
  36. slog.String("feed_url", urllib.RedactedURL(job.FeedURL)),
  37. )
  38. startTime := time.Now()
  39. localizedError := feedHandler.RefreshFeed(w.store, job.UserID, job.FeedID, false)
  40. if config.Opts.HasMetricsCollector() {
  41. status := metric.StatusSuccess
  42. if localizedError != nil {
  43. status = metric.StatusError
  44. }
  45. metric.BackgroundFeedRefreshDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  46. }
  47. }
  48. }