Skip to content

Commit bd2a3dd

Browse files
author
奇淼(piexlmax
authored
清理掉定时任务config配置,修改为代码内书写 (flipped-aurora#1578)
1 parent 4433b66 commit bd2a3dd

File tree

7 files changed

+82
-77
lines changed

7 files changed

+82
-77
lines changed

server/config.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,6 @@ hua-wei-obs:
224224
excel:
225225
dir: ./resource/excel/
226226

227-
# timer task db clear table
228-
Timer:
229-
start: true
230-
spec: "@daily" # 定时任务详细配置参考 https://pkg.go.dev/github.com/robfig/cron/v3
231-
detail:
232-
- tableName: sys_operation_records
233-
compareField: created_at
234-
interval: 2160h
235-
- tableName: jwt_blacklists
236-
compareField: created_at
237-
interval: 168h
238-
239227
# 跨域配置
240228
# 需要配合 server/initialize/router.go -> `Router.Use(middleware.CorsByRules())` 使用
241229
cors:

server/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type Server struct {
2626
AwsS3 AwsS3 `mapstructure:"aws-s3" json:"aws-s3" yaml:"aws-s3"`
2727

2828
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
29-
Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`
3029

3130
// 跨域配置
3231
Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`

server/config/timer.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

server/initialize/timer.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@ package initialize
22

33
import (
44
"fmt"
5+
"github.com/flipped-aurora/gin-vue-admin/server/task"
56

67
"github.com/robfig/cron/v3"
78

8-
"github.com/flipped-aurora/gin-vue-admin/server/config"
99
"github.com/flipped-aurora/gin-vue-admin/server/global"
10-
"github.com/flipped-aurora/gin-vue-admin/server/utils"
1110
)
1211

1312
func Timer() {
14-
if global.GVA_CONFIG.Timer.Start {
15-
for i := range global.GVA_CONFIG.Timer.Detail {
16-
go func(detail config.Detail) {
17-
var option []cron.Option
18-
if global.GVA_CONFIG.Timer.WithSeconds {
19-
option = append(option, cron.WithSeconds())
20-
}
21-
_, err := global.GVA_Timer.AddTaskByFunc("ClearDB", global.GVA_CONFIG.Timer.Spec, func() {
22-
err := utils.ClearTable(global.GVA_DB, detail.TableName, detail.CompareField, detail.Interval)
23-
if err != nil {
24-
fmt.Println("timer error:", err)
25-
}
26-
}, option...)
27-
if err != nil {
28-
fmt.Println("add timer error:", err)
29-
}
30-
}(global.GVA_CONFIG.Timer.Detail[i])
13+
go func() {
14+
var option []cron.Option
15+
option = append(option, cron.WithSeconds())
16+
// 清理DB定时任务
17+
_, err := global.GVA_Timer.AddTaskByFunc("ClearDB", "@daily", func() {
18+
err := task.ClearTable(global.GVA_DB) // 定时任务方法定在task文件包中
19+
if err != nil {
20+
fmt.Println("timer error:", err)
21+
}
22+
}, option...)
23+
if err != nil {
24+
fmt.Println("add timer error:", err)
3125
}
32-
}
26+
27+
// 其他定时任务定在这里 参考上方使用方法
28+
29+
//_, err := global.GVA_Timer.AddTaskByFunc("定时任务标识", "corn表达式", func() {
30+
// 具体执行内容...
31+
// ......
32+
//}, option...)
33+
//if err != nil {
34+
// fmt.Println("add timer error:", err)
35+
//}
36+
}()
3337
}

server/model/common/clearDB.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package common
2+
3+
type ClearDB struct {
4+
TableName string
5+
CompareField string
6+
Interval string
7+
}

server/task/clearTable.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package task
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/flipped-aurora/gin-vue-admin/server/model/common"
7+
"time"
8+
9+
"gorm.io/gorm"
10+
)
11+
12+
//@author: [songzhibin97](https://github.com/songzhibin97)
13+
//@function: ClearTable
14+
//@description: 清理数据库表数据
15+
//@param: db(数据库对象) *gorm.DB, tableName(表名) string, compareField(比较字段) string, interval(间隔) string
16+
//@return: error
17+
18+
func ClearTable(db *gorm.DB) error {
19+
var ClearTableDetail []common.ClearDB
20+
21+
ClearTableDetail = append(ClearTableDetail, common.ClearDB{
22+
TableName: "sys_operation_records",
23+
CompareField: "created_at",
24+
Interval: "2160h",
25+
})
26+
27+
ClearTableDetail = append(ClearTableDetail, common.ClearDB{
28+
TableName: "jwt_blacklists",
29+
CompareField: "created_at",
30+
Interval: "168h",
31+
})
32+
33+
if db == nil {
34+
return errors.New("db Cannot be empty")
35+
}
36+
37+
for _, detail := range ClearTableDetail {
38+
duration, err := time.ParseDuration(detail.Interval)
39+
if err != nil {
40+
return err
41+
}
42+
if duration < 0 {
43+
return errors.New("parse duration < 0")
44+
}
45+
err = db.Debug().Exec(fmt.Sprintf("DELETE FROM %s WHERE %s < ?", detail.TableName, detail.CompareField), time.Now().Add(-duration)).Error
46+
if err != nil {
47+
return err
48+
}
49+
}
50+
return nil
51+
}

server/utils/db_automation.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)