feat:update auto job

This commit is contained in:
zhangwenjian committed 2020-08-12 19:59:04 +08:00
1 parent 509709f3fb
commit e9ef8a2d12
3 files changed
+9 -20

No files matched your search

+4 -2
View File
@@ -2,6 +2,7 @@ package jobs
import (
"fmt"
"time"
)
// 需要将定义的struct 添加到字典中;
@@ -12,10 +13,11 @@ func init() {
jobList = mp
}
// 新添加的job 必须按照以下格式定义,并实现Exec函数
type ExamplesOne struct {
}
func (t ExamplesOne) Exec() {
fmt.Println("call method PrintInfo ,method name : ExamplesOne")
str := time.Now().Format(timeFormat) + " [INFO] JobCore ExamplesOne exec success"
fmt.Println(str)
}
+4 -17
View File
@@ -7,7 +7,7 @@ import (
"go-admin/models"
"go-admin/pkg"
"go-admin/pkg/cronjob"
"reflect"
"sync"
"time"
)
@@ -15,6 +15,7 @@ var timeFormat = "2006-01-02 15:04:05"
var retryCount = 3
var jobList map[string]JobsExec
var lock sync.Mutex
type JobCore struct {
InvokeTarget string
@@ -36,7 +37,9 @@ type ExecJob struct {
func (e *ExecJob) Run() {
startTime := time.Now()
var mp = jobList
lock.Lock()
var obj = mp[e.InvokeTarget]
lock.Unlock()
CallExec(obj.(JobsExec))
//fmt.Println("CallExec exec success")
// 结束时间
@@ -178,19 +181,3 @@ func Stop() chan bool {
}()
return ch
}
func callReflect(any interface{}, name string, args ...interface{}) []reflect.Value {
inputs := make([]reflect.Value, len(args))
for i, _ := range args {
inputs[i] = reflect.ValueOf(args[i])
}
val := reflect.ValueOf(any)
typ := reflect.Indirect(val).Type()
fmt.Println(typ)
if v := reflect.ValueOf(any).MethodByName(name); v.String() == "<invalid Value>" {
return nil
} else {
return v.Call(inputs)
}
}