-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.go
69 lines (56 loc) · 1.31 KB
/
cron.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
package cron
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/applait/xplex-agent/rest"
"github.com/jasonlvhit/gocron"
"github.com/spf13/viper"
)
type Action struct {
Streams []struct {
Name string `json:"name"`
Action string `json:"action"`
} `json:"streams"`
}
func pollRig() {
// Get nginx RTMP stats
nginxRTMPStats, err := rest.GetNginxRTMPStats()
if err != nil {
log.Println(err)
}
// Ping rig with the info and wait for action
actionReq, err := http.NewRequest("POST", viper.GetString("rig.URL")+"action", bytes.NewBuffer(nginxRTMPStats))
actionReq.Header.Set("Content-Type", "application/json")
// Make request
client := &http.Client{}
actionResp, err := client.Do(actionReq)
if err != nil {
log.Println(err)
}
// Parse body
actionBody, err := ioutil.ReadAll(actionResp.Body)
if err != nil {
log.Println(err)
}
defer actionReq.Body.Close()
action := new(Action)
err = json.Unmarshal(actionBody, &action)
if err != nil {
log.Println(err)
}
// Loop through response and act
for _, stream := range action.Streams {
err = act(stream.Name, stream.Action)
if err != nil {
log.Println(err)
}
}
}
// Start starts the CRON which polls rig and take necessary actions on streams
func Start() {
gocron.Every(5).Minutes().Do(pollRig)
gocron.Start()
}