Files
EdgeAPI/internal/tasks/node_log_cleaner_task.go

51 lines
1010 B
Go
Raw Normal View History

2020-10-09 11:06:37 +08:00
package tasks
import (
2024-07-27 14:15:25 +08:00
"time"
2020-10-09 11:06:37 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/iwind/TeaGo/dbs"
2020-10-09 11:06:37 +08:00
)
func init() {
dbs.OnReadyDone(func() {
goman.New(func() {
NewNodeLogCleanerTask(24 * time.Hour).Start()
})
})
2020-10-09 11:06:37 +08:00
}
2021-11-01 15:58:58 +08:00
// NodeLogCleanerTask 清理节点日志的任务
type NodeLogCleanerTask struct {
BaseTask
ticker *time.Ticker
2020-10-09 11:06:37 +08:00
}
func NewNodeLogCleanerTask(duration time.Duration) *NodeLogCleanerTask {
return &NodeLogCleanerTask{
ticker: time.NewTicker(duration),
2020-10-09 11:06:37 +08:00
}
}
func (this *NodeLogCleanerTask) Start() {
for range this.ticker.C {
err := this.Loop()
2020-10-09 11:06:37 +08:00
if err != nil {
this.logErr("NodeLogCleanerTask", err.Error())
2020-10-09 11:06:37 +08:00
}
}
}
func (this *NodeLogCleanerTask) Loop() error {
2022-01-03 10:19:17 +08:00
// 删除 N天 以前的info日志
err := models.SharedNodeLogDAO.DeleteExpiredLogsWithLevel(nil, "info", 3)
2021-11-01 15:58:58 +08:00
if err != nil {
return err
}
2022-01-03 10:19:17 +08:00
// TODO 7天这个数值改成可以设置
return models.SharedNodeLogDAO.DeleteExpiredLogs(nil, 7)
2020-10-09 11:06:37 +08:00
}