-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (191 loc) Β· 3.84 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
wfs "github.com/wenerme/letsgo/fs"
"gopkg.in/yaml.v2"
)
type LogConfig struct {
Level string
File string
Color bool
Format string
Response bool
}
type Config struct {
Jobs []Job `yaml:"jobs"`
Log LogConfig
Conf string
}
type Job struct {
Name string
URL string
Interval time.Duration
Spec string
Log LogConfig
index int
log logrus.FieldLogger
}
var Conf Config
func main() {
app := cli.NewApp()
app.Name = "crontimer"
app.Author = "wener<http://github.com/wenerme>"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "c",
Usage: "config file",
},
}
app.Before = func(ctx *cli.Context) error {
if ctx.String("c") == "" {
if wfs.Exists("./crontimer.yml") {
Conf.Conf = "./crontimer.yml"
} else {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}
Conf.Conf = path.Join(dir, "crontimer.yml")
}
} else {
Conf.Conf = ctx.String("c")
}
b, err := ioutil.ReadFile(Conf.Conf)
if err != nil {
return err
}
err = yaml.Unmarshal(b, &Conf)
if err != nil {
return err
}
if Conf.Log.Level == "" {
Conf.Log.Level = "info"
}
lvl, err := logrus.ParseLevel(Conf.Log.Level)
if err != nil {
return err
}
logrus.SetLevel(lvl)
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: !Conf.Log.Color,
})
if Conf.Log.File != "" {
f, err := os.OpenFile(Conf.Log.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
return err
}
logrus.SetOutput(f)
}
return nil
}
app.Action = Start
app.Commands = []cli.Command{
{
Name: "list",
Action: func(ctx *cli.Context) error {
return template.Must(template.New("list").Parse(`{{"" -}}
Jobs
{{- range $i, $v := .Jobs}}
#{{$i}}
{{if $v.Name -}}
name: {{$v.Name}}
{{end -}}
{{if $v.URL -}}
url: {{$v.URL}}
{{end -}}
{{if $v.Spec -}}
spec: {{$v.Spec}}
{{end -}}
{{if $v.Interval -}}
interval: {{$v.Interval}}
{{end -}}
{{end}}
{{- ""}}`)).Execute(os.Stdout, Conf)
},
},
}
cli.HandleExitCoder(app.Run(os.Args))
}
func Start(*cli.Context) error {
c := cron.New()
for i, j := range Conf.Jobs {
j.index = i
if err := j.Init(); err != nil {
return err
}
if j.Spec == "" {
c.Schedule(j, j)
} else if err := c.AddJob(j.Spec, j); err != nil {
logrus.WithError(err).Warn("AddJob")
}
}
c.Start()
select {}
}
func (j Job) Next(c time.Time) time.Time {
return c.Add(j.Interval)
}
func (j *Job) Init() error {
if j.Log.File != "" {
if j.Log.File == "." {
if j.Name == "" {
j.Log.File = fmt.Sprintf("job.%v.log", j.index)
} else {
j.Log.File = fmt.Sprintf("job.%v.log", j.Name)
}
}
f, err := os.OpenFile(j.Log.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
return err
}
logger := logrus.New()
logger.Out = f
logger.Formatter = &logrus.JSONFormatter{}
l := logger.WithField("url", j.URL)
if j.Name != "" {
l = l.WithField("name", j.Name)
}
j.log = l
} else {
j.log = logrus.WithField("url", j.URL)
}
j.log.Info("Job init")
return nil
}
func (j Job) Run() {
l := j.log
a := time.Now()
r, err := request(j.URL)
elapsed := time.Since(a)
l = l.WithField("used", elapsed)
if err != nil {
l.WithError(err).WithField("url", j.URL).Warn("Request failed")
} else {
if j.Log.Response {
fmt.Println(r)
}
l.WithField("response", r).Info("Request success")
}
}
func request(url string) (string, error) {
resp, err := http.DefaultClient.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}