summaryrefslogtreecommitdiff
path: root/internal/schedule/schedule.go
blob: be2b6f8e89317cc31f7fcd0a3f00d4294fa769fb (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
package schedule

import (
	"errors"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"codeberg.org/snonux/gos/internal/config"
	"codeberg.org/snonux/gos/internal/entry"
	"codeberg.org/snonux/gos/internal/oi"
)

var (
	ErrNothingToSchedule = errors.New("nothing to schedule")
	ErrNothingQueued     = errors.New("nothing queued")
)

func Run(args config.Args, platform string) (entry.Entry, error) {
	dir := fmt.Sprintf("%s/db/platforms/%s", args.GosDir, strings.ToLower(platform))
	stats, err := newStats(dir, args.Lookback, args.Target)
	if err != nil {
		return entry.Zero, err
	}

	log.Println("For", platform, "stats:", stats)
	if stats.targetHit(args.PauseDays) {
		return entry.Zero, ErrNothingToSchedule
	}

	// Schedule random qeued entry for platform
	ent, err := oi.ReadDirRandom(dir, func(file os.DirEntry) (entry.Entry, bool) {
		ent, err := entry.New(filepath.Join(dir, file.Name()))
		if err != nil {
			log.Println(err)
			return entry.Zero, false
		}
		return ent, ent.State == entry.Queued
	})

	if err != nil {
		return entry.Zero, fmt.Errorf("%w: %w", ErrNothingQueued, err)
	}
	return ent, nil
}