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