aboutsummaryrefslogtreecommitdiffhomepage
path: root/storage/job.go
blob: 20a0efa434097b4736e631bc23cb8fa28ae3eea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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"
	"time"

	"github.com/miniflux/miniflux/model"
	"github.com/miniflux/miniflux/timer"
)

const maxParsingError = 3

// NewBatch returns a serie of jobs.
func (s *Storage) NewBatch(batchSize int) (jobs model.JobList, err error) {
	defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetJobs] batchSize=%d", batchSize))
	query := `
		SELECT
		id, user_id
		FROM feeds
		WHERE parsing_error_count < $1
		ORDER BY checked_at ASC LIMIT %d`

	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) {
	defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:GetUserJobs] batchSize=%d, userID=%d", batchSize, userID))
	query := `
		SELECT
		id, user_id
		FROM feeds
		WHERE user_id=$1 AND parsing_error_count < $2
		ORDER BY checked_at ASC LIMIT %d`

	return s.fetchBatchRows(fmt.Sprintf(query, batchSize), userID, maxParsingError)
}

func (s *Storage) fetchBatchRows(query string, args ...interface{}) (jobs model.JobList, err error) {
	rows, err := s.db.Query(query, args...)
	if err != nil {
		return nil, fmt.Errorf("unable to fetch batch of jobs: %v", err)
	}
	defer rows.Close()

	for rows.Next() {
		var job model.Job
		if err := rows.Scan(&job.FeedID, &job.UserID); err != nil {
			return nil, fmt.Errorf("unable to fetch job: %v", err)
		}

		jobs = append(jobs, job)
	}

	return jobs, nil
}