job.go 1.6 KB

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