1
0
Fork 0
miniflux/storage/job.go

83 lines
2.3 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 00:51:50 -04:00
package storage // import "miniflux.app/storage"
2017-11-20 00:10:04 -05:00
import (
"fmt"
"miniflux.app/config"
2018-08-25 00:51:50 -04:00
"miniflux.app/model"
2017-11-20 00:10:04 -05:00
)
2021-05-13 09:38:32 -04:00
// NewBatch returns a series of jobs.
func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
pollingParsingErrorLimit := config.Opts.PollingParsingErrorLimit()
query := `
SELECT
2019-10-30 01:48:07 -04:00
id,
user_id
FROM
feeds
WHERE
disabled is false AND next_check_at < now() AND
CASE WHEN $1 > 0 THEN parsing_error_count < $1 ELSE parsing_error_count >= 0 END
ORDER BY next_check_at ASC LIMIT $2
2019-07-27 00:13:06 -04:00
`
return s.fetchBatchRows(query, pollingParsingErrorLimit, batchSize)
}
2021-05-13 09:38:32 -04:00
// NewUserBatch returns a series of jobs but only for a given user.
func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
// We do not take the error counter into consideration when the given
// user refresh manually all his feeds to force a refresh.
query := `
SELECT
2019-10-30 01:48:07 -04:00
id,
user_id
FROM
feeds
WHERE
user_id=$1 AND disabled is false
ORDER BY next_check_at ASC LIMIT %d
2019-07-27 00:13:06 -04:00
`
return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID)
}
2022-12-11 12:07:01 -05:00
// NewCategoryBatch returns a series of jobs but only for a given category.
func (s *Storage) NewCategoryBatch(userID int64, categoryID int64, batchSize int) (jobs model.JobList, err error) {
// We do not take the error counter into consideration when the given
// user refresh manually all his feeds to force a refresh.
query := `
SELECT
id,
user_id
FROM
feeds
WHERE
user_id=$1 AND category_id=$2 AND disabled is false
ORDER BY next_check_at ASC LIMIT %d
`
return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID, categoryID)
}
func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
rows, err := s.db.Query(query, args...)
2017-11-20 00:10:04 -05:00
if err != nil {
2019-10-30 01:48:07 -04:00
return nil, fmt.Errorf(`store: unable to fetch batch of jobs: %v`, err)
2017-11-20 00:10:04 -05:00
}
defer rows.Close()
for rows.Next() {
var job model.Job
if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
2019-10-30 01:48:07 -04:00
return nil, fmt.Errorf(`store: unable to fetch job: %v`, err)
2017-11-20 00:10:04 -05:00
}
jobs = append(jobs, job)
}
return jobs, nil
2017-11-20 00:10:04 -05:00
}