奇淼(piexlmax
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 197 additions and 2 deletions
-
13server/config.yaml
-
1server/config/config.go
-
13server/config/timer.go
-
4server/global/global.go
-
1server/go.mod
-
24server/initialize/timer.go
-
1server/main.go
-
29server/utils/db_automation.go
-
109server/utils/timer/timed_task.go
@ -0,0 +1,13 @@ |
|||||
|
package config |
||||
|
|
||||
|
type Timer struct { |
||||
|
Start bool `mapstructure:"start" json:"start" yaml:"start"` |
||||
|
Spec string `mapstructure:"spec" json:"spec" yaml:"spec"` |
||||
|
Detail []Detail `mapstructure:"detail" json:"detail" yaml:"detail"` |
||||
|
} |
||||
|
|
||||
|
type Detail struct { |
||||
|
TableName string `mapstructure:"tableName" json:"tableName" yaml:"tableName"` |
||||
|
CompareField string `mapstructure:"compareField" json:"compareField" yaml:"compareField"` |
||||
|
Interval string `mapstructure:"interval" json:"interval" yaml:"interval"` |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package initialize |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"gin-vue-admin/config" |
||||
|
"gin-vue-admin/global" |
||||
|
"gin-vue-admin/utils" |
||||
|
) |
||||
|
|
||||
|
func Timer() { |
||||
|
if global.GVA_CONFIG.Timer.Start { |
||||
|
for _, detail := range global.GVA_CONFIG.Timer.Detail { |
||||
|
fmt.Println(detail) |
||||
|
go func(detail config.Detail) { |
||||
|
global.GVA_Timer.AddTaskByFunc("ClearDB", global.GVA_CONFIG.Timer.Spec, func() { |
||||
|
err := utils.ClearTable(global.GVA_DB, detail.TableName, detail.CompareField, detail.Interval) |
||||
|
if err != nil { |
||||
|
fmt.Println("timer error:", err) |
||||
|
} |
||||
|
}) |
||||
|
}(detail) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package utils |
||||
|
|
||||
|
import ( |
||||
|
"errors" |
||||
|
"fmt" |
||||
|
"time" |
||||
|
|
||||
|
"gorm.io/gorm" |
||||
|
) |
||||
|
|
||||
|
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
|
//@function: ClearTable
|
||||
|
//@description: 清理数据库表数据
|
||||
|
//@param: target db(数据库对象) *gorm.DB,tableName(表名) string,compareField(比较字段) string , interval string 间隔
|
||||
|
//@return: err
|
||||
|
|
||||
|
func ClearTable(db *gorm.DB, tableName string, compareField string, interval string) error { |
||||
|
if db == nil { |
||||
|
return errors.New("db Cannot be empty") |
||||
|
} |
||||
|
duration, err := time.ParseDuration(interval) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
if duration < 0 { |
||||
|
return errors.New("parse duration < 0") |
||||
|
} |
||||
|
return db.Debug().Exec(fmt.Sprintf("DELETE FROM %s WHERE %s < ?", tableName, compareField), time.Now().Add(-duration)).Error |
||||
|
} |
@ -0,0 +1,109 @@ |
|||||
|
package timer |
||||
|
|
||||
|
import ( |
||||
|
"sync" |
||||
|
|
||||
|
"github.com/robfig/cron/v3" |
||||
|
) |
||||
|
|
||||
|
type Timer interface { |
||||
|
AddTaskByFunc(taskName string, spec string, task func()) (cron.EntryID, error) |
||||
|
AddTaskByJob(taskName string, spec string, job interface{ Run() }) (cron.EntryID, error) |
||||
|
FindCron(taskName string) (*cron.Cron, bool) |
||||
|
StartTask(taskName string) |
||||
|
StopTask(taskName string) |
||||
|
Remove(taskName string, id int) |
||||
|
Clear(taskName string) |
||||
|
Close() |
||||
|
} |
||||
|
|
||||
|
// timer 定时任务管理
|
||||
|
type timer struct { |
||||
|
taskList map[string]*cron.Cron |
||||
|
sync.Mutex |
||||
|
} |
||||
|
|
||||
|
// AddTaskByFunc 通过函数的方法添加任务
|
||||
|
func (t *timer) AddTaskByFunc(taskName string, spec string, task func()) (cron.EntryID, error) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
if _, ok := t.taskList[taskName]; !ok { |
||||
|
t.taskList[taskName] = cron.New() |
||||
|
} |
||||
|
id, err := t.taskList[taskName].AddFunc(spec, task) |
||||
|
t.taskList[taskName].Start() |
||||
|
return id, err |
||||
|
} |
||||
|
|
||||
|
// AddTaskByJob 通过接口的方法添加任务
|
||||
|
func (t *timer) AddTaskByJob(taskName string, spec string, job interface{ Run() }) (cron.EntryID, error) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
if _, ok := t.taskList[taskName]; !ok { |
||||
|
t.taskList[taskName] = cron.New() |
||||
|
} |
||||
|
id, err := t.taskList[taskName].AddJob(spec, job) |
||||
|
t.taskList[taskName].Start() |
||||
|
return id, err |
||||
|
} |
||||
|
|
||||
|
// FindCron 获取对应taskName的cron 可能会为空
|
||||
|
func (t *timer) FindCron(taskName string) (*cron.Cron, bool) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
v, ok := t.taskList[taskName] |
||||
|
return v, ok |
||||
|
} |
||||
|
|
||||
|
// StartTask 开始任务
|
||||
|
func (t *timer) StartTask(taskName string) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
if v, ok := t.taskList[taskName]; ok { |
||||
|
v.Start() |
||||
|
} |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// StopTask 停止任务
|
||||
|
func (t *timer) StopTask(taskName string) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
if v, ok := t.taskList[taskName]; ok { |
||||
|
v.Stop() |
||||
|
} |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// Remove 从taskName 删除指定任务
|
||||
|
func (t *timer) Remove(taskName string, id int) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
if v, ok := t.taskList[taskName]; ok { |
||||
|
v.Remove(cron.EntryID(id)) |
||||
|
} |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// Clear 清除任务
|
||||
|
func (t *timer) Clear(taskName string) { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
if v, ok := t.taskList[taskName]; ok { |
||||
|
v.Stop() |
||||
|
delete(t.taskList, taskName) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Close 释放资源
|
||||
|
func (t *timer) Close() { |
||||
|
t.Lock() |
||||
|
defer t.Unlock() |
||||
|
for _, v := range t.taskList { |
||||
|
v.Stop() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func NewTimerTask() Timer { |
||||
|
return &timer{taskList: make(map[string]*cron.Cron)} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue