Files
EdgeAPI/internal/tasks/node_task_extractor.go
GoEdgeLab 5a17ae9d79 v1.4.1
2024-07-27 14:15:25 +08:00

59 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tasks
import (
"time"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/iwind/TeaGo/dbs"
)
func init() {
dbs.OnReadyDone(func() {
goman.New(func() {
NewNodeTaskExtractor(10 * time.Second).Start()
})
})
}
// NodeTaskExtractor 节点任务
type NodeTaskExtractor struct {
BaseTask
ticker *time.Ticker
}
func NewNodeTaskExtractor(duration time.Duration) *NodeTaskExtractor {
return &NodeTaskExtractor{
ticker: time.NewTicker(duration),
}
}
func (this *NodeTaskExtractor) Start() {
for range this.ticker.C {
err := this.Loop()
if err != nil {
this.logErr("NodeTaskExtractor", err.Error())
}
}
}
func (this *NodeTaskExtractor) Loop() error {
// 检查是否为主节点
if !this.IsPrimaryNode() {
return nil
}
// 这里不解锁是为了让任务N秒钟之内只运行一次
for _, role := range []string{nodeconfigs.NodeRoleNode, nodeconfigs.NodeRoleDNS} {
err := models.SharedNodeTaskDAO.ExtractAllClusterTasks(nil, role)
if err != nil {
return err
}
}
return nil
}