Merge pull request #190 from hantmac/master

Add delete log files periodically
This commit is contained in:
Marvin Zhang
2019-08-31 11:44:17 +08:00
committed by GitHub
4 changed files with 70 additions and 0 deletions

View File

@@ -15,6 +15,8 @@ redis:
log:
level: info
path: "/var/logs/crawlab"
isDeletePeriodically: "Y"
deleteFrequency: "@hourly"
server:
host: 0.0.0.0
port: 8000

View File

@@ -29,6 +29,15 @@ func main() {
}
log.Info("初始化日志设置成功")
if viper.GetString("log.isDeletePeriodically") == "Y" {
err := services.InitDeleteLogPeriodically()
if err != nil {
log.Error("Init DeletePeriodically Failed")
panic(err)
}
log.Info("初始化定期清理日志配置成功")
}
// 初始化Mongodb数据库
if err := database.InitMongo(); err != nil {
log.Error("init mongodb error:" + err.Error())

View File

@@ -3,11 +3,15 @@ package services
import (
"crawlab/constants"
"crawlab/database"
"crawlab/lib/cron"
"crawlab/model"
"crawlab/utils"
"encoding/json"
"github.com/apex/log"
"github.com/spf13/viper"
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
)
@@ -71,3 +75,36 @@ func GetRemoteLog(task model.Task) (logStr string, err error) {
return logStr, nil
}
func DeleteLogPeriodically() {
logDir := viper.GetString("log.path")
if !utils.Exists(logDir) {
log.Error("Can Not Set Delete Logs Periodically,No Log Dir")
return
}
rd, err := ioutil.ReadDir(logDir)
if err != nil {
log.Error("Read Log Dir Failed")
return
}
for _, fi := range rd {
if fi.IsDir() {
log.Info(filepath.Join(logDir, fi.Name()))
os.RemoveAll(filepath.Join(logDir, fi.Name()))
log.Info("Delete Log File Success")
}
}
}
func InitDeleteLogPeriodically() error {
c := cron.New(cron.WithSeconds())
if _, err := c.AddFunc(viper.GetString("log.deleteFrequency"), DeleteLogPeriodically); err != nil {
return err
}
c.Start()
return nil
}

View File

@@ -0,0 +1,22 @@
package services
import (
"crawlab/config"
"github.com/apex/log"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"testing"
)
func TestDeleteLogPeriodically(t *testing.T) {
Convey("Test DeleteLogPeriodically", t, func() {
if err := config.InitConfig("../conf/config.yml"); err != nil {
log.Error("init config error:" + err.Error())
panic(err)
}
log.Info("初始化配置成功")
logDir := viper.GetString("log.path")
log.Info(logDir)
DeleteLogPeriodically()
})
}