-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.go
96 lines (88 loc) · 1.96 KB
/
agent.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
/*
* Author:slive
* DATE:2020/9/9
*/
package agent
import (
"flag"
"github.com/slive/gsfly-agent/agent"
config "github.com/slive/gsfly-agent/config"
logx "github.com/slive/gsfly/logger"
"github.com/slive/gsfly/util"
"os"
"os/signal"
"strings"
"syscall"
)
const (
FILE_TYPE_PROPERTIES = ".properties"
FILE_TYPE_YAML = ".yaml"
FILE_TYPE_YML = ".yml"
FILE_TYPE_JSON = ".json"
)
var (
cf string
ft string
agentAddr string
dstAddrs string
pattern string
)
func init() {
flag.StringVar(&cf, "cf", "", "config file path, as'/home/agent.properties'")
}
func RunDef(extension agent.IExtension) {
defer func() {
ret := recover()
if ret != nil {
logx.Panic("run agent error:", ret)
}
}()
flag.Parse()
cf = strings.TrimSpace(cf)
if len(cf) <= 0 {
// 当前目录下或者当前目录的conf下
cf = util.GetPwd() + "/agent.properties"
if !util.CheckFileExist(cf) {
cf = util.GetPwd() + "/conf/agent.properties"
}
}
Run(extension, cf)
}
func Run(extension agent.IExtension, cfPath string) {
logx.Info("properties file:", cfPath)
properties := util.LoadProperties(cfPath)
serviceConfs := config.InitServiceConf(properties)
services := make([]agent.IService, len(serviceConfs))
defer func() {
for _, service := range services {
if service != nil {
service.Stop()
}
}
}()
for index, serviceConf := range serviceConfs {
service := agent.NewService(serviceConf, extension)
extension.SetExtConf(properties)
// 启动
err := service.Start()
if err != nil {
logx.Error("run error:", err)
service.Stop()
panic(err)
}
if extension != nil {
msgHandlers := extension.GetAgentMsgHandlers()
if msgHandlers != nil {
service.GetAgServer().AddMsgHandler(msgHandlers...)
}
}
services[index] = service
}
o := make(chan os.Signal, 1)
signal.Notify(o, os.Kill, os.Interrupt, syscall.SIGABRT, syscall.SIGTERM)
select {
case s := <-o:
logx.Info("stop...., signal:", s)
break
}
}