2019-12-05 22:36:58 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-14 23:53:37 +08:00
|
|
|
"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() {
|
2021-01-19 11:10:32 +08:00
|
|
|
// 初始化 dao 包
|
2022-03-18 23:57:30 +08:00
|
|
|
singleton.Init()
|
2022-04-12 13:16:33 +08:00
|
|
|
singleton.InitConfigFromPath("data/config.yaml")
|
|
|
|
singleton.InitDBFromPath("data/sqlite.db")
|
2022-04-27 23:51:45 +08:00
|
|
|
singleton.InitLocalizer()
|
2021-01-19 09:59:04 +08:00
|
|
|
initSystem()
|
2019-12-08 23:18:29 +08:00
|
|
|
}
|
|
|
|
|
2021-01-19 09:59:04 +08:00
|
|
|
func initSystem() {
|
2022-04-12 13:16:33 +08:00
|
|
|
// 启动 singleton 包下的所有服务
|
|
|
|
singleton.LoadSingleton()
|
2021-01-19 09:59:04 +08:00
|
|
|
|
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)
|
|
|
|
}
|
2021-09-02 23:45:21 +08:00
|
|
|
|
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)
|
|
|
|
}
|
2021-07-14 23:53:37 +08:00
|
|
|
}
|
|
|
|
|
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
|
2021-09-02 23:45:21 +08:00
|
|
|
go rpc.DispatchTask(serviceSentinelDispatchBus)
|
2021-11-11 12:49:54 +08:00
|
|
|
go rpc.DispatchKeepalive()
|
2022-01-09 11:54:14 +08:00
|
|
|
go singleton.AlertSentinelStart()
|
|
|
|
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
|
|
|
|
srv := controller.ServeWeb(singleton.Conf.HTTPPort)
|
2021-07-14 23:53:37 +08:00
|
|
|
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")
|
2021-07-14 23:53:37 +08:00
|
|
|
srv.Shutdown(c)
|
|
|
|
return nil
|
|
|
|
})
|
2019-12-05 22:36:58 +08:00
|
|
|
}
|