File: batch.go

package info (click to toggle)
golang-github-adhocore-gronx 1.19.6-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 248 kB
  • sloc: makefile: 6
file content (51 lines) | stat: -rw-r--r-- 1,025 bytes parent folder | download
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
package gronx

import (
	"strings"
	"time"
)

// Expr represents an item in array for batch check
type Expr struct {
	Err  error
	Expr string
	Due  bool
}

// BatchDue checks if multiple expressions are due for given time (or now).
// It returns []Expr with filled in Due and Err values.
func (g *Gronx) BatchDue(exprs []string, ref ...time.Time) []Expr {
	ref = append(ref, time.Now())
	g.C.SetRef(ref[0])

	var segs []string

	cache, batch := map[string]Expr{}, make([]Expr, len(exprs))
	for i := range exprs {
		batch[i].Expr = exprs[i]
		segs, batch[i].Err = Segments(exprs[i])
		key := strings.Join(segs, " ")
		if batch[i].Err != nil {
			cache[key] = batch[i]
			continue
		}

		if c, ok := cache[key]; ok {
			batch[i] = c
			batch[i].Expr = exprs[i]
			continue
		}

		due := true
		for pos, seg := range segs {
			if seg != "*" && seg != "?" {
				if due, batch[i].Err = g.C.CheckDue(seg, pos); !due || batch[i].Err != nil {
					break
				}
			}
		}
		batch[i].Due = due
		cache[key] = batch[i]
	}
	return batch
}