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.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-22 07:36:00 +01:00
|
|
|
"time"
|
|
|
|
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2018-01-03 04:15:08 +01:00
|
|
|
"github.com/miniflux/miniflux/timer"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxParsingError = 3
|
|
|
|
|
2017-11-22 07:36:00 +01:00
|
|
|
// NewBatch returns a serie of jobs.
|
|
|
|
func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
|
2018-01-03 04:15:08 +01:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetJobs] batchSize=%d", batchSize))
|
2017-11-22 07:36:00 +01:00
|
|
|
query := `
|
|
|
|
SELECT
|
2017-11-20 06:10:04 +01:00
|
|
|
id, user_id
|
|
|
|
FROM feeds
|
|
|
|
WHERE parsing_error_count < $1
|
|
|
|
ORDER BY checked_at ASC LIMIT %d`
|
|
|
|
|
2017-12-23 03:13:14 +01:00
|
|
|
return s.fetchBatchRows(fmt.Sprintf(query, batchSize), maxParsingError)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserBatch returns a serie of jobs but only for a given user.
|
|
|
|
func (s *Storage) NewUserBatch(userID int64, batchSize int) (jobs model.JobList, err error) {
|
2018-01-03 04:15:08 +01:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetUserJobs] batchSize=%d, userID=%d", batchSize, userID))
|
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
|
|
|
|
id, user_id
|
|
|
|
FROM feeds
|
2018-06-27 07:55:44 +02:00
|
|
|
WHERE user_id=$1
|
2017-12-23 03:13:14 +01:00
|
|
|
ORDER BY checked_at ASC LIMIT %d`
|
|
|
|
|
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 {
|
2017-11-22 07:36:00 +01:00
|
|
|
return nil, fmt.Errorf("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 {
|
2017-11-22 07:36:00 +01:00
|
|
|
return nil, fmt.Errorf("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
|
|
|
}
|