Files
EdgeAPI/internal/tasks/node_log_cleaner_task.go

41 lines
778 B
Go
Raw Normal View History

2020-10-09 11:06:37 +08:00
package tasks
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/iwind/TeaGo/dbs"
2020-10-09 11:06:37 +08:00
"github.com/iwind/TeaGo/logs"
"time"
)
func init() {
dbs.OnReady(func() {
go NewNodeLogCleanerTask().Start()
})
2020-10-09 11:06:37 +08:00
}
// 清理节点日志的工具
type NodeLogCleanerTask struct {
2020-10-09 11:06:37 +08:00
duration time.Duration
}
func NewNodeLogCleanerTask() *NodeLogCleanerTask {
return &NodeLogCleanerTask{
2020-10-09 11:06:37 +08:00
duration: 24 * time.Hour,
}
}
func (this *NodeLogCleanerTask) Start() {
2020-10-09 11:06:37 +08:00
ticker := time.NewTicker(this.duration)
for range ticker.C {
err := this.loop()
if err != nil {
logs.Println("[TASK]" + err.Error())
}
}
}
func (this *NodeLogCleanerTask) loop() error {
2020-10-09 11:06:37 +08:00
// TODO 30天这个数值改成可以设置
return models.SharedNodeLogDAO.DeleteExpiredLogs(nil, 30)
2020-10-09 11:06:37 +08:00
}