-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
85 lines (72 loc) · 2.11 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"errors"
"log"
"os"
"time"
"github.com/pcelvng/task-tools"
"github.com/pcelvng/task-tools/bootstrap"
"github.com/pcelvng/task-tools/file/stat"
)
const (
appName = "files"
description = ``
)
func main() {
opts := &options{}
bootstrap.NewTaskMaster(appName, opts.new, opts).
Description(description).Version(tools.String()).
Initialize().
Run()
}
type options struct {
Rules []*Rule `toml:"rule"`
}
func (o options) Validate() error {
if len(o.Rules) == 0 {
return errors.New("no rules provided")
}
// validate each rule
for _, rule := range o.Rules {
if rule.TaskType == "" {
return errors.New("task type required for all rules")
}
if rule.SrcPattern == "" {
return errors.New("src_pattern required for all rules")
}
}
return nil
}
func (appOpt *options) new(app *bootstrap.TaskMaster) bootstrap.Runner {
doneCtx, doneCncl := context.WithCancel(context.Background())
tm := &tskMaster{
initTime: time.Now(),
producer: app.NewProducer(),
consumer: app.NewConsumer(),
appOpt: appOpt,
doneCtx: doneCtx,
doneCncl: doneCncl,
files: make(map[*Rule][]*stat.Stats),
msgCh: make(chan *stat.Stats),
rules: appOpt.Rules,
l: log.New(os.Stderr, "", log.LstdFlags),
}
var err error
// make cron
tm.c, err = makeCron(appOpt.Rules, tm)
if err != nil {
log.Fatal(err)
}
return tm
}
type Rule struct {
TaskType string `toml:"type"` // task type - also default topic
SrcPattern string `toml:"src_pattern"` // source file path pattern to match (supports glob style matching)
InfoTemplate string `toml:"info_template"` // info template (not required)
Topic string `toml:"topic"` // topic override (task type is default)
// checks for rules that checks on groups of files instead of responding
// immediately to an individual file.
CronCheck string `toml:"cron_check"` // optional cron parsable string representing when to check src pattern matching files
CountCheck uint `toml:"count_check"` // optional int representing how many files matching that rule to wait for until the rule is exercised
}