多个API节点时选择一个作为主节点/优化任务相关代码

This commit is contained in:
刘祥超
2022-04-23 12:32:30 +08:00
parent 773f3e1a7e
commit 89c1edc9ee
35 changed files with 467 additions and 350 deletions

View File

@@ -4,40 +4,40 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/logs"
"time"
)
func init() {
dbs.OnReadyDone(func() {
goman.New(func() {
NewNodeLogCleanerTask().Start()
NewNodeLogCleanerTask(24 * time.Hour).Start()
})
})
}
// NodeLogCleanerTask 清理节点日志的任务
type NodeLogCleanerTask struct {
duration time.Duration
BaseTask
ticker *time.Ticker
}
func NewNodeLogCleanerTask() *NodeLogCleanerTask {
func NewNodeLogCleanerTask(duration time.Duration) *NodeLogCleanerTask {
return &NodeLogCleanerTask{
duration: 24 * time.Hour,
ticker: time.NewTicker(duration),
}
}
func (this *NodeLogCleanerTask) Start() {
ticker := time.NewTicker(this.duration)
for range ticker.C {
err := this.loop()
for range this.ticker.C {
err := this.Loop()
if err != nil {
logs.Println("[TASK]" + err.Error())
this.logErr("NodeLogCleanerTask", err.Error())
}
}
}
func (this *NodeLogCleanerTask) loop() error {
func (this *NodeLogCleanerTask) Loop() error {
// 删除 N天 以前的info日志
err := models.SharedNodeLogDAO.DeleteExpiredLogsWithLevel(nil, "info", 3)
if err != nil {