feat: implement cleanup tasks for temporary files and database optimization
This commit is contained in:
41
works/clean.go
Normal file
41
works/clean.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package works
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.lxtend.com/qqbot/sqlite3"
|
||||
"git.lxtend.com/qqbot/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
util.AddCycleTask("cleanTmpFolder", 5*time.Minute, 5*time.Minute, cleanTmpFolder)
|
||||
util.AddCycleTask("cleanDB", 1*time.Hour, 1*time.Hour, cleanDB)
|
||||
}
|
||||
|
||||
func cleanTmpFolder() {
|
||||
//递归获取./tmp/目录下的所有文件
|
||||
files, err := os.ReadDir("./tmp/")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
//删除创建时间超过5分钟的文件
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
info, _ := file.Info()
|
||||
if time.Since(info.ModTime()) > 5*time.Minute {
|
||||
os.RemoveAll(file.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cleanDB() {
|
||||
if time.Now().Weekday() == time.Sunday && time.Now().Hour() < 1 {
|
||||
db := sqlite3.GetDB()
|
||||
db.Exec("VACUUM")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user