job.go 1.6 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
  5. import (
  6. "fmt"
  7. "time"
  8. "github.com/miniflux/miniflux/model"
  9. "github.com/miniflux/miniflux/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. query := `
  27. SELECT
  28. id, user_id
  29. FROM feeds
  30. WHERE user_id=$1 AND parsing_error_count < $2
  31. ORDER BY checked_at ASC LIMIT %d`
  32. return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID, maxParsingError)
  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. }