多个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

@@ -5,7 +5,6 @@ package tasks
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"time"
@@ -14,29 +13,35 @@ import (
func init() {
dbs.OnReadyDone(func() {
goman.New(func() {
NewMonitorItemValueTask().Start()
NewMonitorItemValueTask(1 * time.Hour).Start()
})
})
}
// MonitorItemValueTask 节点监控数值任务
type MonitorItemValueTask struct {
BaseTask
ticker *time.Ticker
}
// NewMonitorItemValueTask 获取新对象
func NewMonitorItemValueTask() *MonitorItemValueTask {
return &MonitorItemValueTask{}
}
func (this *MonitorItemValueTask) Start() {
ticker := time.NewTicker(1 * time.Hour)
func NewMonitorItemValueTask(duration time.Duration) *MonitorItemValueTask {
var ticker = time.NewTicker(duration)
if Tea.IsTesting() {
ticker = time.NewTicker(1 * time.Minute)
}
for range ticker.C {
return &MonitorItemValueTask{
ticker: ticker,
}
}
func (this *MonitorItemValueTask) Start() {
for range this.ticker.C {
err := this.Loop()
if err != nil {
remotelogs.Error("MonitorItemValueTask", err.Error())
this.logErr("MonitorItemValueTask", err.Error())
}
}
}