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-11-20 06:10:04 +01:00
|
|
|
"github.com/miniflux/miniflux2/helper"
|
|
|
|
"github.com/miniflux/miniflux2/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetJobs] batchSize=%d", batchSize))
|
|
|
|
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`
|
|
|
|
|
|
|
|
rows, err := s.db.Query(fmt.Sprintf(query, batchSize), maxParsingError)
|
|
|
|
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
|
|
|
}
|