crontab.go
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package crontab
import (
"context"
"fmt"
"github.com/beego/beego/v2/task"
"github.com/linmadan/egglib-go/utils/xtime"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"io/fs"
"os"
"path/filepath"
"time"
)
type CrontabService struct {
}
func NewCrontabService(options map[string]interface{}) *CrontabService {
newCrontabService := &CrontabService{}
return newCrontabService
}
func (crontabService *CrontabService) initTask() {
autoRemoveExpiredTemporaryFile := task.NewTask("定时清理过期临时文件记录", "0 10 */1 * * *", AutoRemoveExpiredTemporaryFile)
task.AddTask("autoRemoveExpiredTemporaryFile", autoRemoveExpiredTemporaryFile)
autoRemovePublicDownloadFile := task.NewTask("定时清理缓存文件", "0 20 */1 * * *", AutoRemovePublicDownloadFile)
task.AddTask("autoRemovePublicDownloadFile", autoRemovePublicDownloadFile)
}
func (crontabService *CrontabService) StartCrontabTask() {
crontabService.initTask()
task.StartTask()
log.Logger.Info("crontab start!")
}
func (crontabService *CrontabService) StopCrontabTask() {
task.StopTask()
log.Logger.Info("crontab stop!")
}
func AutoRemoveExpiredTemporaryFile(ctx context.Context) error {
defer func() {
if r := recover(); r != nil {
log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时清理过期临时文件记录"})
}
}()
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
if err != nil {
log.Logger.Error("【定时清理过期临时文件记录】 失败:" + err.Error())
}
transactionContext.RollbackTransaction()
}()
log.Logger.Debug("【定时清理过期临时文件记录】 启动")
end := xtime.New(time.Now()).BeginningOfDay().Add(-time.Hour * 6)
begin := end.AddDate(0, 0, -7)
fileRepository, _, _ := factory.FastPgFile(transactionContext, 0)
_, files, err := fileRepository.Find(map[string]interface{}{"limit": 100, "fileType": domain.TemporaryFile.ToString(), "updatedAtBegin": begin, "updatedAtEnd": end})
if err != nil {
return err
}
deleteFileService, _ := factory.CreateDeleteFileService(transactionContext)
if err = deleteFileService.DeleteFiles(&domain.Context{
OperatorName: "系统",
}, files...); err != nil {
return err
}
if err = transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}
func AutoRemovePublicDownloadFile(ctx context.Context) error {
var err error
defer func() {
if r := recover(); r != nil {
log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时清理缓存文件"})
}
}()
defer func() {
if err != nil {
log.Logger.Error("【定时清理缓存文件】 失败:" + err.Error())
}
}()
log.Logger.Debug("【定时清理缓存文件】 启动")
root := "public"
end := xtime.New(time.Now()).BeginningOfDay().AddDate(0, 0, -1)
err = filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
t := info.ModTime()
if !info.IsDir() && xtime.Before(t, end) {
err = os.Remove(path)
if err != nil {
return err
}
log.Logger.Info(fmt.Sprintf("[%v] 删除文件:%v 大小:%v 路径:%v 文件创建时间:%v", "系统", info.Name(), info.Size(), path, info.ModTime()))
return nil
}
return nil
})
if err != nil {
return err
}
return nil
}