-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
65 lines (60 loc) · 1.64 KB
/
run.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
package main
import (
"net/http"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
s "github.com/webtor-io/content-enhancer/services"
)
func makeRunCMD() cli.Command {
cmd := cli.Command{
Name: "run",
Aliases: []string{"r"},
Usage: "runs content enhancement",
Action: run,
}
configureRun(&cmd)
return cmd
}
func configureRun(c *cli.Command) {
c.Flags = s.RegisterClickHouseDBFlags([]cli.Flag{})
c.Flags = s.RegisterUrlBuilderFlags(c.Flags)
}
func run(c *cli.Context) error {
db := s.NewClickHouseDB(c)
ch := s.NewClickHouse(c, db)
ub := s.NewUrlBuilder(c)
recs, err := ch.GetTopContent()
cl := &http.Client{}
if err != nil {
return errors.Wrapf(err, "failed to get stat records")
}
for _, r := range recs {
log.Infof("got record=%+v", r)
du := ub.BuildDoneUrl(&r)
resp, err := cl.Get(du)
if err != nil {
log.WithError(err).Warnf("failed to check done marker url=\"%v\"", du)
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
log.Infof("job already done url=\"%v\" status=\"%v\" code=%v", du, resp.Status, resp.StatusCode)
continue
}
u := ub.BuildInvokeUrl(&r)
log.Infof("invoking url=\"%v\"", u)
resp, err = cl.Get(u)
if err != nil {
log.WithError(err).Warnf("failed to invoke job url=\"%v\"", u)
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
log.Warnf("failed to invoke job url=\"%v\" status=\"%v\" code=%v", u, resp.Status, resp.StatusCode)
} else {
log.Infof("job invoked url=\"%v\" status=\"%v\" code=%v", u, resp.Status, resp.StatusCode)
}
}
return nil
}