-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
91 lines (70 loc) · 1.53 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
package main
import (
"flag"
"net/http"
_ "net/http/pprof"
"os"
"syscall"
"time"
"github.com/dearcode/libbeat/beat"
"github.com/zssky/log"
"github.com/dearcode/kafkabeat/beater"
"github.com/dearcode/kafkabeat/config"
)
func fork(topics string) int {
attr := syscall.ProcAttr{
Env: os.Environ(),
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
}
pid, err := syscall.ForkExec(os.Args[0], []string{os.Args[0], "-t", topics}, &attr)
if err != nil {
panic(err)
}
log.Infof("new child pid:%v, topic:%v", pid, topics)
return pid
}
func watcher(topics string) {
if topics == "" {
return
}
t := time.NewTicker(time.Minute)
pid := fork(topics)
for {
<-t.C
nts, err := config.LoadTopics()
if err != nil {
log.Infof("load config error:%v", err)
continue
}
if nts == topics {
log.Infof("no change:%v", nts)
continue
}
topics = nts
syscall.Kill(pid, syscall.SIGKILL)
var wstatus syscall.WaitStatus
if _, err := syscall.Wait4(pid, &wstatus, 0, nil); err != nil {
log.Errorf("wait pid:%v error:%v", pid, err)
} else {
log.Infof("pid:%v exit status:%v", pid, wstatus)
}
pid = fork(topics)
}
}
func main() {
flag.Parse()
topics, err := config.LoadTopics()
if err != nil {
log.Infof("load config error:%v", err)
return
}
log.Infof("topics:%v", topics)
watcher(topics)
go func() {
log.Infof("%v", http.ListenAndServe(":9900", nil))
}()
if err = beat.Run("kafkabeat", "", beater.New); err != nil {
log.Infof("run beat error:%v", err)
os.Exit(1)
}
}