job.go 1.7 KB

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