job.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package storage // import "miniflux.app/storage"
  5. import (
  6. "fmt"
  7. "miniflux.app/model"
  8. )
  9. const maxParsingError = 3
  10. // NewBatch returns a serie of jobs.
  11. func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
  12. query := `
  13. SELECT
  14. id, user_id
  15. FROM feeds
  16. WHERE parsing_error_count < $1 AND disabled is false
  17. ORDER BY checked_at ASC LIMIT %d
  18. `
  19. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), maxParsingError)
  20. }
  21. // NewUserBatch returns a serie of jobs but only for a given user.
  22. func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
  23. // We do not take the error counter into consideration when the given
  24. // user refresh manually all his feeds to force a refresh.
  25. query := `
  26. SELECT
  27. id, user_id
  28. FROM feeds
  29. WHERE user_id=$1 AND disabled is false
  30. ORDER BY checked_at ASC LIMIT %d
  31. `
  32. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID)
  33. }
  34. func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
  35. rows, err := s.db.Query(query, args...)
  36. if err != nil {
  37. return nil, fmt.Errorf("unable to fetch batch of jobs: %v", err)
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. var job model.Job
  42. if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
  43. return nil, fmt.Errorf("unable to fetch job: %v", err)
  44. }
  45. jobs = append(jobs, job)
  46. }
  47. return jobs, nil
  48. }