/
content_prober.go
180 lines (155 loc) · 4.27 KB
/
content_prober.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
package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
u "net/url"
"os/exec"
"sync"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
"github.com/urfave/cli"
cp "github.com/webtor-io/content-prober/content-prober"
"google.golang.org/grpc"
)
const (
contentProberHostFlag = "content-prober-host"
contentProberPortFlag = "content-prober-port"
contentProberTimeoutFlag = "content-prober-timeout"
)
func RegisterContentProberFlags(c *cli.App) {
c.Flags = append(c.Flags, cli.StringFlag{
Name: contentProberHostFlag,
Usage: "hostname of the content prober service",
EnvVar: "CONTENT_PROBER_SERVICE_HOST",
})
c.Flags = append(c.Flags, cli.IntFlag{
Name: contentProberPortFlag,
Usage: "port of the content prober service",
Value: 50051,
EnvVar: "CONTENT_PROBER_SERVICE_PORT",
})
c.Flags = append(c.Flags, cli.IntFlag{
Name: contentProberTimeoutFlag,
Usage: "probe timeout in seconds",
Value: 600,
EnvVar: "CONTENT_PROBER_TIMEOUT",
})
}
type ContentProbe struct {
host string
port int
timeout int
input string
mux sync.Mutex
err error
r *cp.ProbeReply
inited bool
out string
}
func NewContentProbe(c *cli.Context) *ContentProbe {
return &ContentProbe{
host: c.String(contentProberHostFlag),
port: c.Int(contentProberPortFlag),
timeout: c.Int(contentProberTimeoutFlag),
input: c.String(inputFlag),
out: c.String(OutputFlag),
}
}
func (s *ContentProbe) Get() (*cp.ProbeReply, error) {
s.mux.Lock()
defer s.mux.Unlock()
if s.inited {
return s.r, s.err
}
s.r, s.err = s.get()
s.inited = true
return s.r, s.err
}
func (s *ContentProbe) get() (pr *cp.ProbeReply, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.timeout)*time.Second)
defer cancel()
if s.host == "" {
pr, err = s.localProbe(ctx)
} else {
pr, err = s.remoteProbe(ctx)
}
if err != nil {
return nil, errors.Wrap(err, "Failed to probe")
}
json, err := json.Marshal(pr)
if err != nil {
return nil, errors.Wrap(err, "Failed to convert probe result to json")
}
err = ioutil.WriteFile(s.out+"/index.json", json, 0644)
if err != nil {
return nil, errors.Wrap(err, "Failed to write probe result")
}
return
}
func (s *ContentProbe) remoteProbe(ctx context.Context) (*cp.ProbeReply, error) {
addr := fmt.Sprintf("%s:%d", s.host, s.port)
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
return nil, errors.Wrap(err, "Failed to dial probing service")
}
defer conn.Close()
cl := cp.NewContentProberClient(conn)
req := cp.ProbeRequest{
Url: s.input,
}
log := log.WithField("request", req)
log.Info("Sending probing request")
r, err := cl.Probe(ctx, &req)
if err != nil {
return nil, errors.Wrap(err, "Probing failed")
}
log.WithField("reply", r).Info("Got probing reply")
return r, nil
}
func (s *ContentProbe) localProbe(ctx context.Context) (*cp.ProbeReply, error) {
done := make(chan error)
ffprobe, err := exec.LookPath("ffprobe")
if err != nil {
return nil, errors.Wrap(err, "Unable to find ffprobe")
}
parsedURL, err := u.Parse(s.input)
if err != nil {
return nil, errors.Wrap(err, "Unable to parse url")
}
cmdText := fmt.Sprintf("%s -show_format -show_streams -print_format json '%s'", ffprobe, parsedURL.String())
log.WithField("cmd", cmdText).Info("Running ffprobe command")
cmd := exec.Command(ffprobe, "-show_format", "-show_streams", "-print_format", "json", parsedURL.String())
var bufOut bytes.Buffer
var bufErr bytes.Buffer
cmd.Stdout = &bufOut
cmd.Stderr = &bufErr
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start()
if err != nil {
return nil, errors.Wrap(err, "Unable to start ffprobe")
}
go func() { done <- cmd.Wait() }()
select {
case <-ctx.Done():
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return nil, errors.Wrap(ctx.Err(), "Context done")
case err := <-done:
output := bufOut.String()
stdErr := bufErr.String()
if err != nil {
return nil, errors.Wrapf(err, "Probing failed with err=%v", stdErr)
}
var rep cp.ProbeReply
err = json.Unmarshal([]byte(output), &rep)
if err != nil {
return nil, errors.Wrapf(err, "Unable to unmarshal output=%v", output)
}
log.WithField("output", rep).Info("Probing finished")
return &rep, nil
}
}