-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (119 loc) · 3.19 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
package main
import (
"encoding/json"
"fmt"
"os"
"io/ioutil"
"bufio"
"context"
"io"
"os/exec"
"sync"
"time"
)
type StreamConfig struct {
Interval int
DvrStreams []string
CdnStreams []string
DvrPushApp []string
CdnPushApp []string
}
func main() {
jsonFile,err := os.Open("config.json")
if err != nil{
fmt.Println("Fail to open config.json. err=",err)
return
}
defer jsonFile.Close()
data,err := ioutil.ReadAll(jsonFile)
if err != nil{
fmt.Println("Fail to read config.json in. err=", err)
return
}
var sc StreamConfig
err = json.Unmarshal(data, &sc)
if err != nil {
fmt.Printf("err=%v. exit!\n", err)
return
}
//fmt.Println(sc.DvrStreams)
//fmt.Println(sc.DvrPushApp)
//fmt.Println(sc.CdnStreams)
//fmt.Println(sc.CdnPushApp)
for {
for i,s := range(sc.DvrStreams){
fmt.Println("checking ",s)
ctx, cancel := context.WithCancel(context.Background())
go func(cancelFunc context.CancelFunc) {
time.Sleep(8 * time.Second)
cancelFunc()
//fmt.Println("timeout. checking process quit.")
}(cancel)
Command(ctx, "ffprobe " + s, sc.DvrPushApp[i])
time.Sleep(1*time.Second)
}
for i,s := range(sc.CdnStreams){
fmt.Println("checking ",s)
ctx, cancel := context.WithCancel(context.Background())
go func(cancelFunc context.CancelFunc) {
time.Sleep(8 * time.Second)
cancelFunc()
//fmt.Println("timeout. checking process quit.")
}(cancel)
Command(ctx, "ffprobe " + s, sc.CdnPushApp[i])
time.Sleep(1*time.Second)
}
fmt.Printf("all streams checking is done. next check will be after %d s\n", sc.Interval)
time.Sleep(time.Second * time.Duration(sc.Interval))
}
}
func RunShellCmd(appName string){
cmd := exec.Command("supervisorctl", "restart", appName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil{
fmt.Println(err)
}
}
func read(ctx context.Context, wg *sync.WaitGroup, std io.ReadCloser, appName string) {
reader := bufio.NewReader(std)
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
readString, err := reader.ReadString('\n')
if err != nil || err == io.EOF {
fmt.Println("cmd-pipe exit. err=",ctx.Err())
if ctx.Err() == nil{
fmt.Println("check succeed.")
} else {
fmt.Println("stream is gone. check aborted. restart publisher...")
RunShellCmd(appName)
}
return
}
fmt.Print(readString)
}
}
}
func Command(ctx context.Context, cmd string, appName string) error {
//c := exec.CommandContext(ctx, "cmd", "/C", cmd) // windows
c := exec.CommandContext(ctx, "bash", "-c", cmd) // mac linux
//stdout, err := c.StdoutPipe()
//if err != nil {
// return err
//}
stderr, err := c.StderrPipe()
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(1)
//go read(ctx, &wg, stdout, appName)
go read(ctx, &wg, stderr, appName)
err = c.Start()
wg.Wait()
return err
}