2017-11-20 06:10:04 +01: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 06:51:50 +02:00
|
|
|
package storage // import "miniflux.app/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-22 07:36:00 +01:00
|
|
|
|
2021-01-26 06:41:36 +01:00
|
|
|
"miniflux.app/config"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/model"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2021-05-13 15:38:32 +02:00
|
|
|
// NewBatch returns a series of jobs.
|
2017-11-22 07:36:00 +01:00
|
|
|
func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
|
2021-01-26 06:41:36 +01:00
|
|
|
pollingParsingErrorLimit := config.Opts.PollingParsingErrorLimit()
|
2017-11-22 07:36:00 +01:00
|
|
|
query := `
|
|
|
|
SELECT
|
2019-10-30 06:48:07 +01:00
|
|
|
id,
|
|
|
|
user_id
|
|
|
|
FROM
|
|
|
|
feeds
|
|
|
|
WHERE
|
2021-01-26 06:41:36 +01:00
|
|
|
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 06:13:06 +02:00
|
|
|
`
|
2021-01-26 06:41:36 +01:00
|
|
|
return s.fetchBatchRows(query, pollingParsingErrorLimit, batchSize)
|
2017-12-23 03:13:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 15:38:32 +02:00
|
|
|
// NewUserBatch returns a series of jobs but only for a given user.
|
2017-12-23 03:13:14 +01:00
|
|
|
func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
|
2018-06-27 07:55:44 +02:00
|
|
|
// We do not take the error counter into consideration when the given
|
|
|
|
// user refresh manually all his feeds to force a refresh.
|
2017-12-23 03:13:14 +01:00
|
|
|
query := `
|
|
|
|
SELECT
|
2019-10-30 06:48:07 +01:00
|
|
|
id,
|
|
|
|
user_id
|
|
|
|
FROM
|
|
|
|
feeds
|
|
|
|
WHERE
|
|
|
|
user_id=$1 AND disabled is false
|
2020-05-25 23:06:56 +02:00
|
|
|
ORDER BY next_check_at ASC LIMIT %d
|
2019-07-27 06:13:06 +02:00
|
|
|
`
|
2018-06-27 07:55:44 +02:00
|
|
|
return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID)
|
2017-12-23 03:13:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
|
|
|
|
rows, err := s.db.Query(query, args...)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch batch of jobs: %v`, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var job model.Job
|
|
|
|
if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch job: %v`, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jobs = append(jobs, job)
|
|
|
|
}
|
|
|
|
|
2017-11-22 07:36:00 +01:00
|
|
|
return jobs, nil
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|