crontab.go
5.4 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package crontab
import (
"context"
"fmt"
"github.com/beego/beego/v2/task"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"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/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"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)
autoRemoveTemporaryTable := task.NewTask("定时清理临时表", "0 57 */1 * * *", AutoRemoveTemporaryTable)
task.AddTask("autoRemoveTemporaryTable", autoRemoveTemporaryTable)
}
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
}
func AutoRemoveTemporaryTable(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 * 12)
begin := end.AddDate(0, 0, -7)
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
_, tables, err := tableRepository.Find(map[string]interface{}{"beginTime": begin, "endTime": end, "tableTypes": []string{domain.TemporaryTable.ToString()}})
if err != nil {
return nil
}
for i, t := range tables {
if err = dao.TableDelete(transactionContext.(*pgTransaction.TransactionContext), t.TableId, domain.TemporaryTable); err != nil {
log.Logger.Error(err.Error())
return nil
}
log.Logger.Info(fmt.Sprintf("序号:%d 清理临时表 %v", i, t.SQLName))
if err = starrocks.DropView(starrocks.DB, t.SQLName); err != nil {
log.Logger.Error(err.Error())
return nil
}
}
if err = transactionContext.CommitTransaction(); err != nil {
return err
}
return nil
}