job.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "time"
  8. "miniflux.app/model"
  9. "miniflux.app/timer"
  10. )
  11. const maxParsingError = 3
  12. // NewBatch returns a serie of jobs.
  13. func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
  14. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetJobs] batchSize=%d", batchSize))
  15. query := `
  16. SELECT
  17. id, user_id
  18. FROM feeds
  19. WHERE parsing_error_count < $1
  20. ORDER BY checked_at ASC LIMIT %d`
  21. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), maxParsingError)
  22. }
  23. // NewUserBatch returns a serie of jobs but only for a given user.
  24. func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
  25. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetUserJobs] batchSize=%d, userID=%d", batchSize, userID))
  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, user_id
  31. FROM feeds
  32. WHERE user_id=$1
  33. ORDER BY checked_at ASC LIMIT %d`
  34. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID)
  35. }
  36. func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
  37. rows, err := s.db.Query(query, args...)
  38. if err != nil {
  39. return nil, fmt.Errorf("unable to fetch batch of jobs: %v", err)
  40. }
  41. defer rows.Close()
  42. for rows.Next() {
  43. var job model.Job
  44. if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
  45. return nil, fmt.Errorf("unable to fetch job: %v", err)
  46. }
  47. jobs = append(jobs, job)
  48. }
  49. return jobs, nil
  50. }