调整整体架构写法

调整db用法
调整日志用法
调整模版
This commit is contained in:
linwenxiang
2021-03-04 23:45:16 +08:00
parent 734a48dbb8
commit e2952fa393
137 changed files with 3638 additions and 3544 deletions
+36 -28
View File
@@ -2,6 +2,8 @@ package jobs
import (
"fmt"
log "github.com/go-admin-team/go-admin-core/logger"
"gorm.io/gorm"
"sync"
"time"
@@ -41,7 +43,7 @@ func (e *ExecJob) Run() {
startTime := time.Now()
var obj = jobList[e.InvokeTarget]
if obj == nil {
global.JobLogger.Warn(" ExecJob Run job nil")
log.Warn("[Job] ExecJob Run job nil")
return
}
err := CallExec(obj.(JobsExec), e.Args)
@@ -57,7 +59,7 @@ func (e *ExecJob) Run() {
//TODO: 待完善部分
//str := time.Now().Format(timeFormat) + " [INFO] JobCore " + string(e.EntryId) + "exec success , spend :" + latencyTime.String()
//ws.SendAll(str)
global.JobLogger.Info(time.Now().Format(timeFormat), " [INFO] JobCore ", e, "exec success , spend :", latencyTime)
log.Info("[Job] JobCore %s exec success , spend :%v", e.Name, latencyTime)
return
}
@@ -89,20 +91,26 @@ LOOP:
latencyTime := endTime.Sub(startTime)
//TODO: 待完善部分
global.JobLogger.Info(time.Now().Format(timeFormat), " [INFO] JobCore ", h, "exec success , spend :", latencyTime)
log.Infof("[Job] JobCore %s exec success , spend :%v", h.Name, latencyTime)
return
}
// 初始化
func Setup() {
func Setup(dbs map[string]*gorm.DB) {
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore Starting...")
global.GADMCron = cronjob.NewWithSeconds()
for k, db := range dbs {
global.Cfg.SetCrontab(k, cronjob.NewWithSeconds())
setup(k, db)
}
}
func setup(key string, db *gorm.DB) {
crontab := global.Cfg.GetCrontabKey(key)
sysJob := models.SysJob{}
jobList := make([]models.SysJob, 0)
err := sysJob.GetList(&jobList)
err := sysJob.GetList(db, &jobList)
if err != nil {
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore init error", err)
}
@@ -110,7 +118,7 @@ func Setup() {
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore total:0")
}
_, err = sysJob.RemoveAllEntryID()
_, err = sysJob.RemoveAllEntryID(db)
if err != nil {
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore remove entry_id error", err)
}
@@ -123,7 +131,7 @@ func Setup() {
j.JobId = jobList[i].JobId
j.Name = jobList[i].JobName
sysJob.EntryId, err = AddJob(j)
sysJob.EntryId, err = AddJob(crontab, j)
} else if jobList[i].JobType == 2 {
j := &ExecJob{}
j.InvokeTarget = jobList[i].InvokeTarget
@@ -131,30 +139,30 @@ func Setup() {
j.JobId = jobList[i].JobId
j.Name = jobList[i].JobName
j.Args = jobList[i].Args
sysJob.EntryId, err = AddJob(j)
sysJob.EntryId, err = AddJob(crontab, j)
}
err = sysJob.Update(jobList[i].JobId)
err = sysJob.Update(db, jobList[i].JobId)
}
// 其中任务
global.GADMCron.Start()
crontab.Start()
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore start success.")
// 关闭任务
defer global.GADMCron.Stop()
defer crontab.Stop()
select {}
}
// 添加任务 AddJob(invokeTarget string, jobId int, jobName string, cronExpression string)
func AddJob(job Job) (int, error) {
func AddJob(c *cron.Cron, job Job) (int, error) {
if job == nil {
fmt.Println("unknown")
return 0, nil
}
return job.addJob()
return job.addJob(c)
}
func (h *HttpJob) addJob() (int, error) {
id, err := global.GADMCron.AddJob(h.CronExpression, h)
func (h *HttpJob) addJob(c *cron.Cron) (int, error) {
id, err := c.AddJob(h.CronExpression, h)
if err != nil {
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore AddJob error", err)
return 0, err
@@ -163,8 +171,8 @@ func (h *HttpJob) addJob() (int, error) {
return EntryId, nil
}
func (h *ExecJob) addJob() (int, error) {
id, err := global.GADMCron.AddJob(h.CronExpression, h)
func (h *ExecJob) addJob(c *cron.Cron) (int, error) {
id, err := c.AddJob(h.CronExpression, h)
if err != nil {
fmt.Println(time.Now().Format(timeFormat), " [ERROR] JobCore AddJob error", err)
return 0, err
@@ -174,10 +182,10 @@ func (h *ExecJob) addJob() (int, error) {
}
// 移除任务
func Remove(entryID int) chan bool {
func Remove(c *cron.Cron, entryID int) chan bool {
ch := make(chan bool)
go func() {
global.GADMCron.Remove(cron.EntryID(entryID))
c.Remove(cron.EntryID(entryID))
fmt.Println(time.Now().Format(timeFormat), " [INFO] JobCore Remove success ,info entryID :", entryID)
ch <- true
}()
@@ -185,11 +193,11 @@ func Remove(entryID int) chan bool {
}
// 任务停止
func Stop() chan bool {
ch := make(chan bool)
go func() {
global.GADMCron.Stop()
ch <- true
}()
return ch
}
//func Stop() chan bool {
// ch := make(chan bool)
// go func() {
// global.GADMCron.Stop()
// ch <- true
// }()
// return ch
//}
+3 -1
View File
@@ -1,8 +1,10 @@
package jobs
import "github.com/robfig/cron/v3"
type Job interface {
Run()
addJob() (int, error)
addJob(*cron.Cron) (int, error)
}
type JobsExec interface {