job.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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
  17. ORDER BY checked_at ASC LIMIT %d`
  18. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), maxParsingError)
  19. }
  20. // NewUserBatch returns a serie of jobs but only for a given user.
  21. func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
  22. // We do not take the error counter into consideration when the given
  23. // user refresh manually all his feeds to force a refresh.
  24. query := `
  25. SELECT
  26. id, user_id
  27. FROM feeds
  28. WHERE user_id=$1
  29. ORDER BY checked_at ASC LIMIT %d`
  30. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID)
  31. }
  32. func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
  33. rows, err := s.db.Query(query, args...)
  34. if err != nil {
  35. return nil, fmt.Errorf("unable to fetch batch of jobs: %v", err)
  36. }
  37. defer rows.Close()
  38. for rows.Next() {
  39. var job model.Job
  40. if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
  41. return nil, fmt.Errorf("unable to fetch job: %v", err)
  42. }
  43. jobs = append(jobs, job)
  44. }
  45. return jobs, nil
  46. }