nezha/cmd/dashboard/main.go

57 lines
1.5 KiB
Go
Raw Normal View History

2019-12-05 22:36:58 +08:00
package main
import (
"context"
2022-04-27 23:51:45 +08:00
"log"
2020-11-11 10:07:45 +08:00
"github.com/naiba/nezha/cmd/dashboard/controller"
"github.com/naiba/nezha/cmd/dashboard/rpc"
"github.com/naiba/nezha/model"
2022-01-09 11:54:14 +08:00
"github.com/naiba/nezha/service/singleton"
2022-04-12 13:16:33 +08:00
"github.com/ory/graceful"
2019-12-05 22:36:58 +08:00
)
2019-12-08 16:59:58 +08:00
func init() {
// 初始化 dao 包
2022-04-12 13:16:33 +08:00
singleton.InitConfigFromPath("data/config.yaml")
2022-10-12 23:06:25 +08:00
singleton.InitTimezoneAndCache()
2022-04-12 13:16:33 +08:00
singleton.InitDBFromPath("data/sqlite.db")
2022-04-27 23:51:45 +08:00
singleton.InitLocalizer()
initSystem()
2019-12-08 23:18:29 +08:00
}
func initSystem() {
2022-04-12 13:16:33 +08:00
// 启动 singleton 包下的所有服务
singleton.LoadSingleton()
2022-04-11 22:51:02 +08:00
// 每天的3:30 对 监控记录 和 流量记录 进行清理
2022-04-12 13:16:33 +08:00
if _, err := singleton.Cron.AddFunc("0 30 3 * * *", singleton.CleanMonitorHistory); err != nil {
2021-07-19 10:37:12 +08:00
panic(err)
}
2022-04-11 22:51:02 +08:00
// 每小时对流量记录进行打点
2022-04-12 13:16:33 +08:00
if _, err := singleton.Cron.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
2021-07-19 10:37:12 +08:00
panic(err)
}
}
2019-12-08 16:59:58 +08:00
func main() {
2022-04-12 13:16:33 +08:00
singleton.CleanMonitorHistory()
2022-01-09 11:54:14 +08:00
go rpc.ServeRPC(singleton.Conf.GRPCPort)
2022-04-11 22:51:02 +08:00
serviceSentinelDispatchBus := make(chan model.Monitor) // 用于传递服务监控任务信息的channel
go rpc.DispatchTask(serviceSentinelDispatchBus)
go rpc.DispatchKeepalive()
2022-01-09 11:54:14 +08:00
go singleton.AlertSentinelStart()
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
srv := controller.ServeWeb(singleton.Conf.HTTPPort)
graceful.Graceful(func() error {
return srv.ListenAndServe()
}, func(c context.Context) error {
2021-11-06 18:40:29 +08:00
log.Println("NEZHA>> Graceful::START")
2022-04-12 13:16:33 +08:00
singleton.RecordTransferHourlyUsage()
2021-11-06 18:40:29 +08:00
log.Println("NEZHA>> Graceful::END")
srv.Shutdown(c)
return nil
})
2019-12-05 22:36:58 +08:00
}