nezha/cmd/dashboard/main.go

55 lines
1.5 KiB
Go
Raw Normal View History

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