-
Notifications
You must be signed in to change notification settings - Fork 2
/
task.go
97 lines (82 loc) · 2.03 KB
/
task.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
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/macrat/ayd/internal/ayderr"
"github.com/macrat/ayd/internal/scheme"
"github.com/macrat/ayd/internal/store"
api "github.com/macrat/ayd/lib-ayd"
"github.com/robfig/cron/v3"
)
var (
ErrInvalidArgument = errors.New("invalid argument")
)
type Task struct {
Schedule Schedule
Prober scheme.Prober
}
func (t Task) MakeJob(ctx context.Context, s *store.Store) cron.Job {
return cron.FuncJob(func() {
defer func() {
if err := recover(); err != nil {
s.Report(t.Prober.Target(), api.Record{
Time: time.Now(),
Target: t.Prober.Target(),
Status: api.StatusUnknown,
Message: fmt.Sprintf("panic: %s", err),
})
}
}()
t.Prober.Probe(ctx, s)
})
}
func (t Task) SameAs(another Task) bool {
return t.Schedule.String() == another.Schedule.String() && t.Prober.Target().String() == another.Prober.Target().String()
}
func (t Task) In(list []Task) bool {
for _, x := range list {
if t.SameAs(x) {
return true
}
}
return false
}
func ParseArgs(args []string) ([]Task, error) {
var tasks []Task
errors := &ayderr.ListBuilder{What: ErrInvalidArgument}
schedule := DEFAULT_SCHEDULE
for _, a := range args {
if s, err := ParseSchedule(a); err == nil {
schedule = s
continue
}
p, err := scheme.NewProber(a)
if err != nil {
switch err {
case scheme.ErrUnsupportedScheme:
errors.Pushf("%s: This scheme is not supported. Please check if the plugin is installed if need.", a)
case scheme.ErrMissingScheme:
errors.Pushf("%s: Not valid as schedule or target URL. Please specify scheme if this is target. (e.g. ping:%s or http://%s)", a, a, a)
case scheme.ErrInvalidURL:
errors.Pushf("%s: Not valid as schedule or target URL.", a)
default:
errors.Pushf("%s: %w", a, err)
}
continue
}
tasks = append(tasks, Task{
Schedule: schedule,
Prober: p,
})
}
var result []Task
for _, t := range tasks {
if t.In(result) {
continue
}
result = append(result, t)
}
return result, errors.Build()
}