job.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/v2/internal/storage"
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/internal/config"
  7. "miniflux.app/v2/internal/model"
  8. )
  9. // NewBatch returns a series of jobs.
  10. func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
  11. pollingParsingErrorLimit := config.Opts.PollingParsingErrorLimit()
  12. query := `
  13. SELECT
  14. id,
  15. user_id
  16. FROM
  17. feeds
  18. WHERE
  19. disabled is false AND next_check_at < now() AND
  20. CASE WHEN $1 > 0 THEN parsing_error_count < $1 ELSE parsing_error_count >= 0 END
  21. ORDER BY next_check_at ASC LIMIT $2
  22. `
  23. return s.fetchBatchRows(query, pollingParsingErrorLimit, batchSize)
  24. }
  25. // NewUserBatch returns a series of jobs but only for a given user.
  26. func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
  27. // We do not take the error counter into consideration when the given
  28. // user refresh manually all his feeds to force a refresh.
  29. query := `
  30. SELECT
  31. id,
  32. user_id
  33. FROM
  34. feeds
  35. WHERE
  36. user_id=$1 AND disabled is false
  37. ORDER BY next_check_at ASC LIMIT %d
  38. `
  39. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID)
  40. }
  41. // NewCategoryBatch returns a series of jobs but only for a given category.
  42. func (s *Storage) NewCategoryBatch(userID int64, categoryID int64, batchSize int) (jobs model.JobList, err error) {
  43. // We do not take the error counter into consideration when the given
  44. // user refresh manually all his feeds to force a refresh.
  45. query := `
  46. SELECT
  47. id,
  48. user_id
  49. FROM
  50. feeds
  51. WHERE
  52. user_id=$1 AND category_id=$2 AND disabled is false
  53. ORDER BY next_check_at ASC LIMIT %d
  54. `
  55. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID, categoryID)
  56. }
  57. func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
  58. rows, err := s.db.Query(query, args...)
  59. if err != nil {
  60. return nil, fmt.Errorf(`store: unable to fetch batch of jobs: %v`, err)
  61. }
  62. defer rows.Close()
  63. for rows.Next() {
  64. var job model.Job
  65. if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
  66. return nil, fmt.Errorf(`store: unable to fetch job: %v`, err)
  67. }
  68. jobs = append(jobs, job)
  69. }
  70. return jobs, nil
  71. }