-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.go
67 lines (58 loc) · 1.76 KB
/
web.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
package cmd
import (
"context"
"log"
"github.com/ory/graceful"
"github.com/urfave/cli"
"github.com/midoks/nezha/cmd/dashboard/controller"
"github.com/midoks/nezha/cmd/dashboard/rpc"
"github.com/midoks/nezha/model"
"github.com/midoks/nezha/service/singleton"
)
var Web = cli.Command{
Name: "web",
Usage: "This command starts web services",
Description: `Start Web services`,
Action: runWebService,
}
func initWeb() {
// 初始化 dao 包
singleton.InitConfigFromPath("data/config.yaml")
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath("data/sqlite.db")
singleton.InitLocalizer()
initSystem()
}
func initSystem() {
// 启动 singleton 包下的所有服务
singleton.LoadSingleton()
// 每天的3:30 对 监控记录 和 流量记录 进行清理
if _, err := singleton.Cron.AddFunc("0 30 3 * * *", singleton.CleanMonitorHistory); err != nil {
panic(err)
}
// 每小时对流量记录进行打点
if _, err := singleton.Cron.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
panic(err)
}
}
func runWebService(c *cli.Context) error {
initWeb()
singleton.CleanMonitorHistory()
go rpc.ServeRPC(singleton.Conf.GRPCPort)
serviceSentinelDispatchBus := make(chan model.Monitor) // 用于传递服务监控任务信息的channel
go rpc.DispatchTask(serviceSentinelDispatchBus)
go rpc.DispatchKeepalive()
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
srv := controller.ServeWeb(singleton.Conf.HTTPPort)
graceful.Graceful(func() error {
return srv.ListenAndServe()
}, func(c context.Context) error {
log.Println("NEZHA>> Graceful::START")
singleton.RecordTransferHourlyUsage()
log.Println("NEZHA>> Graceful::END")
srv.Shutdown(c)
return nil
})
return nil
}